4

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);}
3
  • please add the type of currentStatusTimestamp and some values. Commented Dec 15, 2018 at 16:20
  • FWIW, unless you've some really good reason to represent those timestamps as strings, you should convert them into numbers before you ever get this far. Commented Dec 15, 2018 at 16:23
  • The type of currentStatusTimestamp, is integer, I just used the casting to Number through my debugging process to ensure that even if the timestamps had been saved in DB as strings, that was not the problem. I will remove it. Thanks for the tip! Commented Dec 16, 2018 at 6:53

1 Answer 1

1

The JS .sort function requires a comparator that returns a negative number, zero, or a positive number. Your second returns a boolean.

Your first function fails because you do (a < b) ? 1 : (b > a) ? -1 : 0, but the two comparisons are equivalent, i.e. you got the second comparison the wrong way around.

It normally suffices to just return the difference between the two numeric values:

.sort((a, b) => +a[1].currentStatusTimestamp - +b[1].currentStatusTimestamp);

This will give the results in ascending order. Swap a and b to reverse that.

Sign up to request clarification or add additional context in comments.

4 Comments

dammit, I hate scrollbars that are invisible most of the time. I didn't see the rest of the OP's first function.
no need for casting to number with unary plus. the minus makes it implicit.
@NinaScholz I never use implicit casts. That's how the WAT?! stuff happens. I suppose strictly speaking the unary plus is also an implicit cast, but it's a well behaved one.
Thanks a lot that worked! Concerning the casting, ESLint suggests to use explicit casting to number using Number().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.