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