JQUERY Tablesorter没有正确排序数字列

时间:2014-09-30 10:46:07

标签: javascript jquery tablesorter

我在排序包含浮点数和整数的列时遇到问题:

示例

当前正在对列进行排序:

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

我想按值对列进行排序,因为它似乎是按字符长度对数字进行排序 - 可能吗?

这是我的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>

有人可以让我知道我需要做些什么来纠正这个问题? 感谢

3 个答案:

答案 0 :(得分:2)

修复是由@ fuchs777提出的。 usNumberFormat设置设置为false。这意味着tableorter将这些值视为德国数字格式,例如1.234.567,89。使用德语数字格式,数千个由句点(句号)表示,而不是逗号。

该修复程序正在设置usNumberFormat : true

例如:

$(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;
    }
    });
});

答案 1 :(得分:2)

你设置usNumberFormat为false,然后分拣机应该期望数值如430,95 ...要么改变你的值,要使用“,”作为小数分隔符或使用

usNumberFormat : true

答案 2 :(得分:0)

在进行任何计算或比较之前,您无法将值a和b转换为使用parseFloat浮点数吗?

a = parseFloat(a);
b = parseFloat(b);
相关问题