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);
a[0][2]is undefined in your case. Same as ab[0][2].a[0][1]andb[0][1]