Javascript 按字符串对包含数组的数组进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5435228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:11:14  来源:igfitidea点击:

Sort an array with arrays in it by string

javascriptarrayssorting

提问by dmnkhhn

I have an array that contains several arrays and I would like to order the arrays based on a certain string within those arrays.

我有一个包含多个数组的数组,我想根据这些数组中的某个字符串对数组进行排序。

var myArray = [
                [1, 'alfred', '...'],
                [23, 'berta', '...'],
                [2, 'zimmermann', '...'],
                [4, 'albert', '...'],
              ];

How can I sort it by the name so that albertcomes first and zimmermanncomes last?

如何按名称对其进行排序,以便阿尔伯特排在第一位,齐默尔曼排在最后?

I know how I would do it if I could use the integer for sorting but the string leaves me clueless.

我知道如果我可以使用整数进行排序,我会怎么做,但字符串让我一无所知。

Thank for your help! :)

感谢您的帮助!:)

回答by Martin Milan

This can be achieved by passing a supporting function as an argument to the Array.sortmethod call.

这可以通过将支持函数作为参数传递给Array.sort方法调用来实现。

Something like this:

像这样的东西:

 function Comparator(a, b) {
   if (a[1] < b[1]) return -1;
   if (a[1] > b[1]) return 1;
   return 0;
 }

 var myArray = [
   [1, 'alfred', '...'],
   [23, 'berta', '...'],
   [2, 'zimmermann', '...'],
   [4, 'albert', '...'],
 ];

 myArray = myArray.sort(Comparator);
 console.log(myArray);

回答by vhallac

You can still use array.sort()with a custom function. Inside the function, simply compare the element that you want to use as your key. For you example, you could use:

您仍然可以使用array.sort()自定义函数。在函数内部,只需比较要用作键的元素即可。例如,您可以使用:

myArray.sort(function(a, b) { 
    return a[1] > b[1] ? 1 : -1;
});

回答by luschn

There′s an easier way now:

现在有一个更简单的方法:

myArray = myArray.sort(function(a, b) {
    return a[1].localeCompare(b[1]);
})

It is case insensitive too.

它也不区分大小写。

Source: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare

来源:https: //developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare

回答by Brian M. Hunt

In ES6 one might do the relatively pithy:

在 ES6 中,可能会做相对简洁的:

myArray.sort(([a], [b]) => a.localeCompare(b))

or

或者

myArray.sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0)

The helpful/modern bits being the =>lambda operator, and the [X]argument destructuring.

有用/现代的位是=>lambda 运算符和[X]参数解构。

回答by gordon

Awesome! Compound sort on first element, second element and then third, all ascending in this example, would be

惊人的!对第一个元素、第二个元素和第三个元素进行复合排序,在本例中全部升序,将是

myArray=myArray.sort(function(a,b){
    retVal=0;
    if(a[0]!=b[0]) retVal=a[0]>b[0]?1:-1;
    else if(a[1]!=b[1]) retVal=a[1]>b[1]?1:-1;
    else if(a[2]!=b[2]) retVal=a[2]>b[2]?1:-1;
    return retVal
});

(multiple sort on more than one column)

(对多于一列进行多重排序)