I've found myself using @NickGrealy method to sort items and it works great!
(https://stackoverflow.com/a/49041392/18045902)
Issue I had is that i'm using a different format for date: dd-mm-YY instead of the ISO one.
As I'm passing data from a .php file as a string I had to convert the string in a date then compare with ><==
Substitute the compare function
// Returns a function responsible for sorting a specific column index
// (idx = columnIndex, asc = ascending order?).
var comparer = function(idx, asc) {
// This is used by the array.sort() function...
return function(a, b) {
// This is a transient function, that is called straight away.
// It allows passing in different order of args, based on
// the ascending/descending order.
return function(v1, v2) {
// sort based on a numeric or localeCompare, based on type...
return (v1 !== '' && v2 !== '' && !isNaN(v1) && !isNaN(v2))
? v1 - v2
: v1.toString().localeCompare(v2);
}(getCellValue(asc ? a : b, idx), getCellValue(asc ? b : a, idx));
}
};
with this:
var comparer = function(idx, asc) {
// This is used by the array.sort() function...
return function(a, b) {
// This is a transient function, that is called straight away.
// It allows passing in different order of args, based on
// the ascending/descending order.
return function(v1, v2) {
if(v1 !== '' && v2 !== '' && !isNaN(v1) && !isNaN(v2)){
x = v1 - v2;
console.log(v1);
} else if(v1.includes("-")) {
var partsArray1 = v1.split('-');
var partsArray2 = v2.split('-');
var data1 = new Date(partsArray1[2],partsArray1[1],partsArray1[0]);
var data2 = new Date(partsArray2[2],partsArray2[1],partsArray2[0]);
if(data1>data2){
x=1;
} else if (data1<data2) {
x=-1;
} else if (data1==data2) {
x=0;
}
} else {
x = v1.toString().localeCompare(v2);
}
// sort based on a numeric or localeCompare, based on type...
//return (v1 !== '' && v2 !== '' && !isNaN(v1) && !isNaN(v2))
// ? v1 - v2
// : v1.toString().localeCompare(v2);
return x;
}(getCellValue(asc ? a : b, idx), getCellValue(asc ? b : a, idx));
}
};
NOTE: this will only work if the date you are trying to parse is a string in the dd-mm-YY format.
If you need a different format change the includes() and the split() character (in my case is "-") and the order of the date you are creating with Date().
If there's something wrong with this method please comment.