2

I am trying to order some div's by a number in its content. I used this code:

jQuery("#Overzicht > div").sort(function(a, b) {
    var upA = jQuery('.distance-order', a).text().toUpperCase();
    var upB = jQuery('.distance-order', b).text().toUpperCase();
    return upA < upB; }).appendTo('#Overzicht');

I found this code at this item: How to sort nested divs depending upon content

But I just have a problem with getting it ordered at number (with decimals). You can find the whole set up over here: http://jsfiddle.net/84GRA/

Community
  • 1
  • 1
Peter Hut
  • 33
  • 4

2 Answers2

1

You used toUpperCase() on a number which won't do anything. To check for comparison between numbers, you need to parse it first with parseFloat()

Fiddle

Aashray
  • 2,753
  • 16
  • 22
0

Use:

jQuery("#Overzicht > div").sort(function(a, b) {
    var upA = parseFloat(jQuery('.distance-order', a).text());
    var upB = parseFloat(jQuery('.distance-order', b).text());
    return upB - upA;
}).appendTo('#Overzicht');

This replaces the toUpperCase with parseFloat, and returns a proper value from the sort function.

If you want to sort them in the other direction, simply switch upA and upB:

return upA - upB;
Cerbrus
  • 70,800
  • 18
  • 132
  • 147