0

I have an array like this:

let pairs = [[2,"test"], [7,"buzz"], [3,"asd"]]

How can i sort the outer array by the first values? (descending)

1 Answers1

2

Pass a compare function to Array.sort that examines the first element in each array, to determine the required order.

> let pairs = [[2,"test"], [7,"buzz"], [3,"asd"]]
> pairs.sort((a, b) => a[0] - b[0]);
[ [ 2, 'test' ], [ 3, 'asd' ], [ 7, 'buzz' ] ]
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61