Customized Natural Sort not working...
Customized Natural Sort not working...
Hi all,
I'm trying to write a customized sorting method for one of my columns. It's pretty straight-forward really, but for some reason it's not working properly. Basically what I want to do is check if the text contains the word "TOTALS". If so, display it as the first/last item in the list (depending on whether it's ascending/descending), otherwise just do a standard string comparison.
So I've copied the naturalSort() method from here: http://www.overset.com/2008/09/01/javascript-natural-sort-algorithm-with-unicode-support/
And I've added this code to my customSorters.js file:
[code]
jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"custom-title-pre": function ( a ) {
return a;
},
"custom-title-asc": function ( a, b ) {
if (a.indexOf("TOTALS")!=-1) return 1;
if (b.indexOf("TOTALS")!=-1) return -1;
return naturalSort(a,b);
},
"custom-title-desc": function ( a, b ) {
if (a.indexOf("TOTALS")!=-1) return -1;
if (b.indexOf("TOTALS")!=-1) return 1;
return naturalSort(a,b)*-1;
}
} );
[/code]
And my column is defined as:
[code]{"sTitle": "Titulo", "sType": "custom-title"}[/code]
The TOTALS part works fine. I see it as always the first or last row in the table, but the internal order of the other rows does not seem to be sorted as it should be, alphabetically.
Any ideas on what might be going wrong, or any suggestions on other ways of doing it?
Thanks!
I'm trying to write a customized sorting method for one of my columns. It's pretty straight-forward really, but for some reason it's not working properly. Basically what I want to do is check if the text contains the word "TOTALS". If so, display it as the first/last item in the list (depending on whether it's ascending/descending), otherwise just do a standard string comparison.
So I've copied the naturalSort() method from here: http://www.overset.com/2008/09/01/javascript-natural-sort-algorithm-with-unicode-support/
And I've added this code to my customSorters.js file:
[code]
jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"custom-title-pre": function ( a ) {
return a;
},
"custom-title-asc": function ( a, b ) {
if (a.indexOf("TOTALS")!=-1) return 1;
if (b.indexOf("TOTALS")!=-1) return -1;
return naturalSort(a,b);
},
"custom-title-desc": function ( a, b ) {
if (a.indexOf("TOTALS")!=-1) return -1;
if (b.indexOf("TOTALS")!=-1) return 1;
return naturalSort(a,b)*-1;
}
} );
[/code]
And my column is defined as:
[code]{"sTitle": "Titulo", "sType": "custom-title"}[/code]
The TOTALS part works fine. I see it as always the first or last row in the table, but the internal order of the other rows does not seem to be sorted as it should be, alphabetically.
Any ideas on what might be going wrong, or any suggestions on other ways of doing it?
Thanks!
This discussion has been closed.