I have a table like this I'd like to sort :
| Name | Case | | John | X-123/开发者_JAVA百科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:
$.tablesorter.addParser({
// set a unique id
id: 'case',
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(/^X-\d{1,4}\/(\d{2}).*$/, '$1') + ('000' + s.replace(/^X-(\d{1,4})\/\d{2}.*$/, '$1')).slice(-4);
},
// set type, either numeric or text
type: 'text'
});
EDIT:
Maybe you already tried something like this with type: 'numeric'
; I'm not sure, but this may fail because parseInt('09') === 0
.
EDIT 2:
Changed to reflect sorting by year, then case number.
You can use a regex to get the 2 values you're after, for example:
var columnVal = "X-123/08";
var m = columnVal.match(/X\-(\d+)\/(\d{2})$/);
var case = m[1];
var year = m[2];
After trying multiple things this one seems to work perfectly well :
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 caseVar = '000' + m[1];
var size = caseVar.length;
caseVar= caseVar.substr(size-4, 4);
var year = m[2];
return jQuery.tablesorter.formatFloat(year + caseVar);
},
// set type, either numeric or text
type: 'numeric'
});
I did some research and it appears that the substr function with a negative value doesn't work on all browser (I'm using IE because my application must be IE compliant). I think that's the cause of my previous problems and that's why Spiny's solution wasn't working for me.
Thanks anyway to all of you for your precious help !
精彩评论