3

I've one function for sort div by price ASC and DESC. But it doesn't work on Safari. It's ok on Firefox/Chrome.

What's the reason?

The code (and a fiddle version):

  function sortByPrice(a,b){
      return $(a).find('.cicerone_price').text() > $(b).find('.cicerone_price').text();
  }

  function sortByPriceDesc(a,b){
     return $(a).find('.cicerone_price').text() < $(b).find('.cicerone_price').text();
  }

  function reorderEl(el){
      var container = $('#tabs');
      container.html('');
      el.each(function(){
          $(this).appendTo(container);
      });
  }
  $('#filter_price').change(function(){
      if ($("#filter_price option:selected").val()=='desc'){ 
          reorderEl($('.global_product').sort(sortByPriceDesc));
         } else if ($("#filter_price option:selected").val()=='asc'){ 
            reorderEl($('.global_product').sort(sortByPrice));
            }
  });
brasofilo
  • 25,496
  • 15
  • 91
  • 179
artSx
  • 1,560
  • 1
  • 12
  • 19
  • Safari does not seem to like your returning a boolean value from your comparison functions for sorting. Return -1/1 instead (and maybe 0 for elements with equal sort criterion). – CBroe Apr 08 '13 at 14:08
  • do you have any issue for that ? i'm lost -_- – artSx Apr 08 '13 at 15:28
  • I updated your fiddle – works fine for me that way in Safari 5.1.7/Win7. – CBroe Apr 09 '13 at 08:34
  • you can copy/paste the code ? I cant see on my fiddle link the update ?! Thx a lot ! – artSx Apr 09 '13 at 08:40
  • 1
    Sorry, I thought it updated, but seems it didn’t. Have a look here: http://jsfiddle.net/3kcwW/1/ – CBroe Apr 09 '13 at 08:44
  • i found other problem, when you put one price width 230$, 21$ and 150$. Sorting is => 230$ => 21$ => 150$. http://jsfiddle.net/3kcwW/3/ – artSx Apr 09 '13 at 09:14
  • Well, that happens when you lexically compare _strings_ instead of numbers. So you should put the price info into an element where you can read it from without the $ sign and then parse it into a number before comparing. – CBroe Apr 09 '13 at 09:17
  • how i can do that ? i'm novice in jquery :( – artSx Apr 09 '13 at 09:20
  • Native JavaScript offers the methods `parseInt`/`parseFloat` to convert strings containing numerical data into real numbers. – CBroe Apr 09 '13 at 09:42
  • ok, i check on google what is it. But i dont know how i can implemant on my jquery ?! – artSx Apr 09 '13 at 09:48
  • thx for the help CBroe, i found my error, my fiddle corrected : http://jsfiddle.net/3kcwW/8/ – artSx Apr 10 '13 at 14:40
  • @artSx If you solved the problem, you should post the solution as an answer and then accept it. Then it will help future visitors to the site. – Taryn Apr 10 '13 at 14:44

1 Answers1

2

The problem has been solved, for the solution, i add parseFloat for decimal

solution is here : fiddle correction

function sortByPrice(a,b){
    return parseFloat($(a).find('.productPriceForSorting').text()) > parseFloat($(b).find('.productPriceForSorting').text()) ? 1 : -1;
}
artSx
  • 1,560
  • 1
  • 12
  • 19