2014-09-30 35 views
1

我在整理包含浮點數和整數列問題: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> 

可能有人請讓我知道我需要做糾正呢? 謝謝

+1

只是一個猜測,但你設置usNumberFormat假,然後分揀機應該會像430,95數值...要麼改變你的價值觀,或者使用真 – fuchs777 2014-09-30 10:53:35

+2

@ fuchs777 - 偉大的建議!這是現在的工作,謝謝 – aphextwix 2014-09-30 10:59:18

+0

爲什麼你需要'numberSorter'中額外的代碼?沒有它,它應該正常工作。 – Mottie 2014-10-02 17:34:43

回答

2

此修復程序由@ fuchs777提出。 usNumberFormat設置設置爲false。這意味着tablesorter將這些值視爲德文數字格式,例如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; 
    } 
    }); 
}); 
+0

沒有任何變化。還有其他建議嗎?謝謝 – aphextwix 2014-09-30 10:54:19

+0

你可以顯示標記嗎? – Donal 2014-09-30 10:59:53

+0

感謝您的幫助Donal。 – aphextwix 2014-09-30 11:01:25

0

在進行任何計算或比較之前,您不能將值a和b轉換爲使用parseFloat浮動嗎?

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

設置usNumberFormat假,然後分揀機應該會像430,95數值...要麼改變你的價值觀使用」, 「作爲小數點分隔符或使用

usNumberFormat : true