[removed] Sort array of arrays by second element in each inner array

后端 未结 2 505
眼角桃花
眼角桃花 2020-12-07 03:06

I have an array that looks like this:

const arr = [
  [500, \'Foo\'],
  [600, \'bar\'],
  [700, \'Baz\'],
];

I would like to sort this

相关标签:
2条回答
  • 2020-12-07 03:43

    Here is a concrete, working example, using Array.prototype.sort:

    const arr = [
      [500, 'Foo'],
      [600, 'bar'],
      [700, 'Baz']
    ];
    
    arr.sort((a,b) => a[1].toUpperCase().localeCompare(b[1].toUpperCase()));
    
    console.log(arr);

    0 讨论(0)
  • 2020-12-07 03:48

    Array.prototype.sort takes a function which will be applied to each pair of items in the array. The return of that function determines how the items are sorted (it needs to return a positive number, 0, or a negative number).

    0 讨论(0)
提交回复
热议问题