Given this data, my sort function will sort by ascending date, ascending time, and in alphabetical order by first name:
[
['Kurt Asdf', '25 Nov 2017 4:30 PM'],
['Vincent Qwerty', '25 Nov 2017 4:30 PM'],
['Zed Jones', '24 Nov 2017 2:00 PM'],
['Jack Mo', '25 Nov 2017 5:00 PM'],
['John Phil', '25 Nov 2017 4:00 PM'],
['Bob Phil', '25 Nov 2017 4:00 PM']
]
Here is the output from my current sort function (code below):
[
['Zed Jones', '24 Nov 2017 2:00 PM'],
['Bob Phil', '25 Nov 2017 4:00 PM'],
['John Phil', '25 Nov 2017 4:00 PM'],
['Kurt Asdf', '25 Nov 2017 4:30 PM'],
['Vincent Qwerty', '25 Nov 2017 4:30 PM'],
['Jack Mo', '25 Nov 2017 5:00 PM']
]
Here is my desired output:
[
['Bob Phil', '25 Nov 2017 4:00 PM'],
['John Phil', '25 Nov 2017 4:00 PM'],
['Kurt Asdf', '25 Nov 2017 4:30 PM'],
['Vincent Qwerty', '25 Nov 2017 4:30 PM'],
['Jack Mo', '25 Nov 2017 5:00 PM'],
['Zed Jones', '24 Nov 2017 2:00 PM']
]
Notice how in the above output the dates are in descending order yet the times are in ascending order and the alphabetical order within each time slot is maintained.
Here is what I have tried:
function sortTable(data) {
return data.sort((elem1, elem2) => {
var dateA = new Date(elem1[1])
, dateB = new Date(elem2[1])
, nameA = elem1[0]
, nameB = elem2[0]
, datecomp = dateB-dateA;
if (nameA === undefined || nameB === undefined)
namecomp = 0;
else
namecomp = nameA[0] > nameB[0] || -(nameA[0] < nameB[0]);
return datecomp > 0 ? datecomp : datecomp + namecomp;
});
}
elem1[5]when the elements you are passing in seem to be arrays with two entries? - CBroe