0

I want to sort an array like this:

var myArray = [
  ['red', 350],
  ['yellow', 20],
  ['green', 75],
  ['blue', 100]
];

In the order of the numbers. So the output should be:

var myArray = [
  ['red', 350],
  ['blue', 100],
  ['green', 75],
  ['yellow', 20]
];

I tried using .sort but I couldn't get it to work right. Thanks!

  • 1
    `myArray.sort( ([,a],[,b]) => b - a )` – Paul Feb 20 '20 at 22:10
  • Does this answer your question? [How to sort an array by a date property](https://stackoverflow.com/questions/10123953/how-to-sort-an-array-by-a-date-property) – Terry Chern Feb 20 '20 at 22:20

1 Answers1

0

You need to specify a custom sort function:

myArray.sort((a,b) => b[1] - a[1]);
Vadim Sirbu
  • 483
  • 3
  • 9