2

I wish to sort the arrays by the time (which is the 3rd element). However, when I print out the result, the sequence of the array seems to have no difference.

Can I know what's the error? Thank you!

const events =[
  [ 1, '1230000003', '09:00:00' ],
  [ -1, '1230000003', '14:00:00' ],
  [ -1, '1110000002', '12:30:00' ],
  [ 1, '1110000002', '14:00:00' ],
  [ -1, '1110000007', '08:30:00' ],
  [ 1, '1110000007', '11:00:00' ],
  [ -1, '1110000008', '09:00:00' ],
  [ 1, '1110000008', '12:00:00' ]
]

This is my code:

 events.sort((a, b) => a[0][2] - b[0][2]);
 console.log("sorted start time",events);
2
  • Hm. a[0][2] is undefined in your case. Same as a b[0][2]. Commented Jul 15, 2020 at 3:01
  • @VasylMoskalov I see.. but it seems okay when i do it to other elements like a[0][1] and b[0][1] Commented Jul 15, 2020 at 3:12

3 Answers 3

2

You could use localCompare

const events =[ [ 1, '1230000003', '09:00:00' ], [ -1, '1230000003', '14:00:00' ], [ -1, '1110000002', '12:30:00' ], [ 1, '1110000002', '14:00:00' ], [ -1, '1110000007', '08:30:00' ], [ 1, '1110000007', '11:00:00' ], [ -1, '1110000008', '09:00:00' ], [ 1, '1110000008', '12:00:00' ] ]
events.sort((a, b) => a[2].localeCompare(b[2]));
console.log("sorted start time",events);

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

2 Comments

yes this solution works for my code! However when I run the code, may I know why this will come first [ 1, "1230000003", "09:00:00" ], instead of this [ -1, "1110000008", "09:00:00" ], ?
that's because since the function compares time, both values in this case are equal, and since the first value in the first array is passed first it will show first in the result does that makes since ?
2

Since it is date is string , you should use the sort function as

const events =[
  [ 1, '1230000003', '09:00:00' ],
  [ -1, '1230000003', '14:00:00' ],
  [ -1, '1110000002', '12:30:00' ],
  [ 1, '1110000002', '14:00:00' ],
  [ -1, '1110000007', '08:30:00' ],
  [ 1, '1110000007', '11:00:00' ],
  [ -1, '1110000008', '09:00:00' ],
  [ 1, '1110000008', '12:00:00' ]
]

events.sort((a, b) => a[2].localeCompare(b[2]));
console.log(events);

2 Comments

thanks! this actually helps! but can I know why when it comes to same timing like [ 1,'1230000003', '09:00:00' ] and [ -1, '1110000008', '09:00:00' ], the first one will come first instead of second one, what if i want this [ -1, '1110000008', '09:00:00' ] to come first?
you can customize the logic to check if the field you are checking on is same , then compare based on the other one you mentioned
1

Change this;

 events.sort((a, b) => a[0][2] - b[0][2]);

To this:

events.sort((a, b) => a[2] > b[2] ? 1 : -1);

Result:

[
  [-1, "1110000007", "08:30:00"],
  [-1, "1110000008", "09:00:00"],
  [1, "1230000003", "09:00:00"],
  [1, "1110000007", "11:00:00"],
  [1, "1110000008", "12:00:00"],
  [-1, "1110000002", "12:30:00"],
  [1, "1110000002", "14:00:00"],
  [-1, "1230000003", "14:00:00"]
]

Comments

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.