通过串排序与数组的数组在其中 [英] Sort an array with arrays in it by string
本文介绍了通过串排序与数组的数组在其中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含多个阵列,我想根据这些阵列内的某些字符串命令数组的数组。
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 albert comes first and zimmermann comes last?
我知道我会怎么做,如果我可以用整数进行排序,但字符串让我无言以对。
I know how I would do it if I could use the integer for sorting but the string leaves me clueless.
感谢您的帮助! :)
推荐答案
这可以通过将支撑作用作为参数传递给的Array.Sort
方法调用来实现的。
This can be achieved by passing a supporting function as an argument to the Array.Sort
method call.
事情是这样的:
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);
我不是一个JavaScript奇才顺便说一句 - 谨慎使用...
I am not a Javascript wiz btw - use caution ...
这篇关于通过串排序与数组的数组在其中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文