Reputation: 10294
Requirement
My table can have values like these -
1.00
0.00
23.10
-
35.00
-
22.00
0.00
0.00
I would it to be sorted like this (all hyphens should be in the end) -
1.00
22.00
23.10
35.00
0.00
0.00
0.00
-
-
Or even this will be fine (all hyphens in the beginning, at least they are separate from the others) -
-
-
0.00
0.00
0.00
1.00
22.00
23.10
35.00
What I did
I am using a custom sorter like this (this is the one we use in our project and it is working perfectly fine for integers and decimal numbers, i.e. float types) -
$.tablesorter.addParser({
// set a unique id
id: 'formattedNumbers',
is: function(s){
// return false so this parser is not auto detected
return false;
},
format: function(s){
// format your data for normalization
return s.replace(/,/g, '').replace(/%/g, ''); //removes comma separator from formatted numbers
},
// set type, either numeric or text
type: 'numeric'
});
And calling it like this -
$("#"+report_type).tablesorter({
indexing: true,
textExtraction: 'complex',
headers : 1: {
{ sorter: 'formattedNumbers' },
},
widgets :['zebra'],
sortList:[[0,0]]
});
Result I am getting
But I am getting unpredictable results. The numbers other than 0.00 and hyphen (-) get sorted correctly among themselves, but some of the 0.00 and hyphen remain scattered randomly. Like this -
0.00
1.00
22.00
0.00
-
23.10
-
35.00
0.00
-
My thoughts
We need to change the logic inside the custom sorter so as to handle the hyphen character. I tried looking for Natural sorting as suggested in jQuery tablesorter - sorting a column with mixed text and numbers but did not find any working custom sorter like our "formattedNumbers".
Upvotes: 3
Views: 1460
Reputation: 1
You can do array and use sort method:
tab.sort(function(a,b){
if(b==0){
return -1
}
else if(a==0){
return 1
}
else{
return (a-b)
}
})
Upvotes: 0
Reputation: 1891
Friend, do change in your add parser code.
Old code
format: function(s){
// format your data for normalization
return s.replace(/,/g, '').replace(/%/g, ''); //removes comma separator from formatted numbers
}
After the suggestions given by Mottie.
Updated code
format: function(s){
// format your data for normalization
s = s.replace(/,/g, '').replace(/%/g, ''); //removes comma, percentage
if(s && s.trim() && s.trim().length==1){
s = s.replace(/-/g,'');
}
return s;
}
Hope, this will solve your problem. In the scenario given by you(values contains more zeros and there is no negative number appearing), to make it look more beautiful you can replace '-' with -1 to separate it from zeros.
Updated version (if you are sure there will be no data<0):
format: function(s){
// format your data for normalization
s = s.replace(/,/g, '').replace(/%/g, ''); //removes comma, percentage
if(s && s.trim() && s.trim().length==1){
s = s.replace(/-/g,'-1');
}
return s;
}
Cheers.
Upvotes: 1
Reputation: 86433
Since no HTML was shared, we can only assume that the hypens in the markup don't include extra spaces, tabs or other invisible characters. The tablesorter parser is set as "numeric" but a string is being returned, so the comparison may not be correct.
As @Xiaodoudou showed in the jsFiddle example, my fork of tablesorter will sort the hypens properly.
In addition, you can make non-numerical data (strings) sort at the bottom of the column - see this demo. No extra parser is needed and all you would need to do is add a sort-bottom
class to the column's <th>
- try out this demo.
<th class="string-bottom">Numeric</th>
Upvotes: 1
Reputation: 326
The default sorter method of tablesorter will give you
0.00
0.00
0.00
1.00
22.00
23.10
35.00
-
-
Or
-
-
35.00
23.10
22.00
1.00
0.00
0.00
0.00
take a look here: http://jsfiddle.net/Y6eMv/2/
I don't understand the goal of having that result :
1.00
22.00
23.10
35.00
0.00
0.00
0.00
-
-
Could give more detail of what you are trying to achieve?
Upvotes: 1