I have a table like this I’d like to sort :
| Name | Case |
| John | X-123/08 P|
| Bob | X-123/09 |
| Dylan | X-45/10 |
I want to sort the Case colum by case’s year then case’s number knowing that the format is always “X-(1 to 4 digits for case’s number)/(case’s year on 2 digits) (sometimes some text)”. It’s possible that after the year’s case I have some text but it shoud be ignored for sorting.
I am using tablesorter jQuery’s plugin and I am struggling to add a custom parser for this.
Thanks for your help !
EDIT : Here’s what I’m trying to do :
jQuery.tablesorter.addParser({
// set a unique id
id: 'case',
is: function(s) {
return false;
},
format: function(s) {
// format your data for normalization
return s.replace(/^X-\d{1,4}\/(\d{2}).*$/, '$1') + ('000' + s.replace(/^X-(\d{1,4})\/\d{2}.*$/, '$1')).substr(-4);
},
// set type, either numeric or text
type: 'text'
});
It’s working great until I encounter a case with 2 digits which is then ranked greater than a 3 digits one and I don’t understand why …
“X-458/09 P” is sorted smaller than “X-48/09” . I’ll try some debug to see what really happens.
EDIT 2 : Also tried the second answer :
jQuery.tablesorter.addParser({
// set a unique id
id: 'case',
is: function(s) {
return false;
},
format: function(s) {
var m = s.match(/X\-(\d+)\/(\d{2}).*$/);
var affaire = m[1];
var year = m[2];
return year + '000' + affaire;
},
// set type, either numeric or text
type: 'text'
});
The result seems to be the same as the first one… I really can’t understand why it sucks. Why tablesorter thinks that 488 000 10 is smaller than 49 000 10 ?!
I think you can use:
EDIT:
Maybe you already tried something like this with
type: 'numeric'; I’m not sure, but this may fail becauseparseInt('09') === 0.EDIT 2:
Changed to reflect sorting by year, then case number.