JSFiddle - React, Tailwind, and code Playground
JavaScript
/*Function Sort array multicolumn
It's a beggining because the sort inside an array is very complicated
For the moment we can only, sort by column, ASC or DESC by string, numeric or date(no case sensitive).
*/
if( typeof helper == 'undefined' ) {
var helper = { } ;
}
helper.arr = {
/**
* Function to sort multidimensional array
*
* <a href="/param">@param</a> {array} [arr] Source array
* <a href="/param">@param</a> {array} [columns] List of columns to sort
* <a href="/param">@param</a> {array} [order_by] List of directions (ASC, DESC)
* @returns {array}
*/
multisort: function(arr, columns, order_by) {
if(typeof columns == 'undefined') {
columns = []
for(x=0;x<arr[0].length;x++) {
columns.push(x);
}
}
if(typeof order_by == 'undefined') {
order_by = []
for(x=0;x<arr[0].length;x++) {
order_by.push('ASC');
}
}
function IsDate(testValue) {
var returnValue = false;
var testDate;
try {
testDate = new Date(testValue);
console.log(testDate);
console.log(!isNaN(testDate));
if (!isNaN(testDate)) {
returnValue = true;
} else {
returnValue = false;
}
}
catch (e) {
returnValue = false;
}
return returnValue;
}
function multisort_recursive(a,b,columns,order_by,index) {
var direction = order_by[index] == 'DESC' ? 1 : 0;
var is_date = (IsDate(a[columns[index]]) && IsDate(b[columns[index]]))
var is_numeric = (!isNaN(+a[columns[index]] - +b[columns[index]]));
if(is_numeric){ /*Numeric*/
var x = +a[columns[index]];
var y = +b[columns[index]];
}else if (is_date){ /*Date*/
...