I am trying to sort an array which contains arrays of two elements of the form [[string, object], [string, object], ...]. Each object has a property named: currentStatusTimestamp. I am trying to sort the internal arrays based on this property. It seems that all the elements are sorted correctly except one element which stays always at the first position of the array.
You can view an example describing the problem below.
Unsorted: Unsorted array
Sorted: Sorted array
The first element at each line of the images corresponds to arr[0] (string) and the second to the arr[1].currentStatusTimestamp.
I have tried to sort the arrays with two different functions, but neither seem to work.
You may find the two functions below:
const sortByCurrentStatusTimestamp2 = (array) => {
array.sort((a,b) => {
return (Number(a[1].currentStatusTimestamp) < Number(b[1].currentStatusTimestamp)) ? 1 : ((Number(b[1].currentStatusTimestamp) > Number(a[1].currentStatusTimestamp)) ? -1 : 0);} );}
const sortByCurrentStatusTimestamp = (array) => {
array.sort((a, b) => {
return Number(a[1].currentStatusTimestamp) < Number(b[1].currentStatusTimestamp);
});}
The code which calls the functions is displayed below:
sortByCurrentStatusTimestamp2(entriesArray);
or
sortByCurrentStatusTimestamp(entriesArray);
You may find the code used to display the output which is shown in the images, below:
for (let e of entriesArray) {
console.log(e[0],e[1].currentStatusTimestamp);}
sortByCurrentStatusTimestamp2(entriesArray);
for (let e of entriesArray) {
console.log(e[0],e[1].currentStatusTimestamp);}
currentStatusTimestampand some values.