Reputation: 4332
I'm using the current master of https://github.com/Mottie/tablesorter, which I've updated to from the older tablesorter for the ability to the use the Filter widget. But in doing so, I've run across an issue where it seems to fail sorting digits with leading 0's, when asked to use the 'text' sorter.
Example:
headers:
0:
sorter: 'text'
On a column with values
I am expecting a sorted value to be
But instead it appears to be parsing it as a digit as the leading 0 has no affect on the sort order.
jsFiddles:
I've created a basic jsFiddle here: https://jsfiddle.net/ewjg8mob/ to display my need.
Another jsFiddle to show that the sorting is 'working' but appears to be stripping the leading 0's, which to me means it's sorting as a digit.
I've tried this with the most recent version jQuery 1, 2 and 3.
NOTE The 'old' Tablesorter did this correctly, but the newer fork seems to be having issues.
Upvotes: 1
Views: 508
Reputation: 86433
The fork of tablesorter replaced the basic text sort with a natural sort algorithm, so values like 01
and 0001
are parsed and treated as the same value.
To get around this, you can set the textSorter
option to use a basic text sort - the same as the original version - as follows (demo)
$(function() {
$("#tablesorter-example").tablesorter({
textSorter: {
1: $.tablesorter.sortText
},
headers: {
1: { sorter: 'text' }
}
});
});
Alternatively, you can create a custom parser to modify the cached value to tweak the sort order (demo)
$(function() {
$.tablesorter.addParser({
id: 'leadingZeros',
is: function() {
return false;
},
format: function(s, table) {
var number = parseFloat(s),
str = number.toString();
if (s.length !== str.length) {
// subtract a decimal equivalent of the string length
// so "0001" sorts before "01"
number -= 0.00000001 * (s.length - str.length);
}
return number;
},
type: 'number'
});
$("#tablesorter-example").tablesorter({
headers: {
1: { sorter: 'leadingZeros' }
}
});
});
Upvotes: 1