Javascript:按每个内部数组中的第二个元素对数组排序 [英] Javascript: Sort array of arrays by second element in each inner array
本文介绍了Javascript:按每个内部数组中的第二个元素对数组排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个如下所示的数组:
I have an array that looks like this:
const arr = [
[500, 'Foo'],
[600, 'bar'],
[700, 'Baz'],
];
我想按字母顺序对此 arr
进行排序通过每个内部数组中的第二个元素,即:
I would like to sort this arr
alphabetically by the second element in each inner array, ie:
[
[600, 'bar'],
[700, 'Baz'],
[500, 'Foo'],
]
请注意不区分大小写。另外,如果它们在这里派上用场,我很乐意使用lodash助手!
Note the case insensitivity. Also, I would love to use lodash helpers if they come in handy here!
推荐答案
这是一个具体的工作示例,使用 Array.prototype.sort
:
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);
这篇关于Javascript:按每个内部数组中的第二个元素对数组排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文