1

I'm having problems sorting columns that contain floating point numbers and integers:

Example

Column currently being sorted like so :

4697.2
403.95
399.38
317.94
316.44
3138.7
308.28
262.75
1839.5
179.94
159.97
145.99
103.95
94.95
90.24
819.9

I would like to sort the column by value as it appears to be sorting the figures by character length at the moment - possibly?

Here's my javascript :

<script>

$(function(){
    $('table').tablesorter({
        widgets        : ['zebra', 'columns', 'stickyHeaders'],
        usNumberFormat : false,
        numberSorter: function (a, b, direction) {
    if (a >= 0 && b >= 0) { return direction ? a - b : b - a; }
    if (a >= 0) { return -1; }
    if (b >= 0) { return 1; }
    return direction ? b - a : a - b;
}
    });
});

</script>

Could somebody please let me know what I need to do to correct this? Thanks

2
  • 1
    just a guess, but you set usNumberFormat false, the sorter should then expect numeric values like 430,95... either change your values or use true Commented Sep 30, 2014 at 10:53
  • Why do you need the extra code in the numberSorter? It should work properly without it. Commented Oct 2, 2014 at 17:34

3 Answers 3

2

The fix was suggested by @fuchs777. The usNumberFormat setting was set to false. Which meant the tablesorter was treating the values as if they were in German number format, e.g. 1.234.567,89. With German number format the thousands are denoted by periods (full stops) and not commas.

The fix was setting usNumberFormat : true

For example:

$(function(){
    $('table').tablesorter({
        widgets        : ['zebra', 'columns', 'stickyHeaders'],
        usNumberFormat : true,
        numberSorter: function (a, b, direction) {
        if (a >= 0 && b >= 0) { return direction ? a - b : b - a; }
        if (a >= 0) { return -1; }
        if (b >= 0) { return 1; }
        return direction ? b - a : a - b;
    }
    });
});
Sign up to request clarification or add additional context in comments.

4 Comments

Nothing has changed. Any other suggestions? Thanks
@fuchs777 suggested the answer: usNumberFormat : true.
@fuchs777 Yes it would. When usNumberFormat was set to false - it was assuming the values were German number format. In German number format thousands are denoted by periods and not commas.
@Donal after years of using js I still don't trust that automatic type casting ;)
2

you set usNumberFormat false, the sorter should then expect numeric values like 430,95... either change your values to use "," as decimal separator or use

usNumberFormat : true

Comments

0

Can't you convert the values a and b to floats with parseFloat before doing any calculation or comparison?

a = parseFloat(a);
b = parseFloat(b);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.