I am using this great version of jQuery Tablesorter: http://mottie.github.com/tablesorter/docs/index.html
Everything is working well but now I have this problem: in my table, I have one column which contains positions of basketball players. Therefore I want that column to be sorted logically like this: PG-SG-SF-PF-C.
I tried to create this custom sorting function – look at my script, column 2:
$(document).ready(function() {
$(".stats").tablesorter({
sortInitialOrder: 'desc',
sortRestart: true,
// Enable use of the characterEquivalents reference
sortLocaleCompare: false,
// if false, upper case sorts BEFORE lower case
ignoreCase: true,
headers: {
0: {
sortInitialOrder: 'asc'
},
1: {
sortInitialOrder: 'asc'
},
2: {
textSorter: function(a, b){
var positions = {
"PG": 0,
"SG": 10,
"SF": 20,
"PF": 30,
"C": 40
};
return ((positions[a] < positions[b]) ? -1 : ((positions[a] > positions[b]) ? 1 : 0));
},
sortInitialOrder: 'asc'
}
}
}
);
});
However, the column is still being sorted alphabetically like a normal text string (C-PF-PG-SF-SG).
Where am I making a mistake? I am not particularly strong in Javascript so it is probably somewhere in the sorting function. Thank you.
I figured it out by adding my own parser like it is shown in this question: Sorting Image and hyperlink columns in a table using JQuery Sorter plugin
I will copy my script that works as I want it to work, hope it helps someone: