Open Faizanq opened 5 years ago
In my case, this ended up being there were tab characters and such messing up the sorting function.
I was able to correct it like this. The jQuery use is a little unnecessary in my example, but I was doing additional transformations in my specific case that I've left out of the below.
This is pretty much an exact copy of what list.js does for its normal sorting, but ensures that tab characters and any additional spacing is ignored.
// Webpack will recognize the copy of this included by list.js and will hardly impact your compiled JS size when including it again
var naturalSort = require( 'string-natural-compare' );
let list = new List(
htmlElement,
{
valueNames: [
'column1',
'column2'
],
page: 15,
pagination: true,
listClass: 'someCSSClass',
sortFunction: function( itemA, itemB, options ) {
var sort = naturalSort;
sort.alphabet = options.alphabet || undefined;
if ( ! sort.alphabet && options.insensitive) {
sort = naturalSort.caseInsensitive;
}
var $itemA = $( $.parseHTML( itemA.values()[ options.valueName ] ) ),
$itemB = $( $.parseHTML( itemB.values()[ options.valueName ] ) );
return sort( $itemA.text().trim(), $itemB.text().trim() );
}
}
);
Sorting not persistent when doing pagination it remains its actual state when going to other pages