I want to use tablesorter to sort a column (column contains first name + last name) so that clicking the header toggles between sorting by first name and sorting by last name, both in ascending order. I have found a jsfiddle (link) that is very close to what I want to do.
$(function () {
var $cell;
$('#games').tablesorter({
theme: 'blue',
textExtraction: {
0: function (node, table, cellIndex) {
var $n = $(node);
// add semi-colon between values
return $n.find('.name').text() + ';' + $n.find('.perc').text();
}
},
textSorter: function (a, b) {
var x = a.split(';'),
y = b.split(';'),
i = $cell && $cell.is('.active') ? 1 : 0;
return $.tablesorter.sortNatural($.trim(x[i]), $.trim(y[i]));
},
initialized: function () {
$cell = $('#games').find('.percSort');
$cell.click(function () {
// activate percentage sort
$cell.addClass('active');
return false;
}).closest('th').click(function () {
// clicking on header outside of percSort
// inactivates the percentage sort
$cell.removeClass('active');
});
}
});
});
It toggles between sorting by name (ascending) and sorting by percentage (descending) when the header is clicked. However, in my case, I want it to toggle between sorting first name (ascending) and last name (ascending). I want both sorts to be ascending and I'm not sure how to modify this code to make that happen. If nothing else, I can make them separate columns instead and give up on this, but I think it would look better to have the full name displayed together.
I tried looking through the documentation, but I wasn't able to figure out a way.
Just keep the html structure as it is (), and change just its text-content: