Javascript 在javascript中对多维数组进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2824145/
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
Sorting a multidimensional array in javascript
提问by Kyle Hotchkiss
How would you sort a multidimensional array in JavaScript?
你会如何在 JavaScript 中对多维数组进行排序?
I have an array full of arrays that contain two dates and a string. I need the main array sorted by one of the date arrays, is this possible?
我有一个充满数组的数组,其中包含两个日期和一个字符串。我需要按日期数组之一排序的主数组,这可能吗?
data stucture:
数据结构:
events = [
{ date 1, date 2, string },
{ date 2, date 2, string },
]
回答by Sean Kinsey
Duplicate of sort outer array based on values in inner array, javascripthere you will find several answers, like my own
基于内部数组中的值排序外部数组的重复,javascript在这里你会找到几个答案,就像我自己的
var arr = [.....]
arr.sort((function(index){
return function(a, b){
return (a[index] === b[index] ? 0 : (a[index] < b[index] ? -1 : 1));
};
})(2)); // 2 is the index
This sorts on index 2
这对索引 2 进行排序
回答by Anurag
The array structure seems a little vague from your description. You can use a custom sort function to compare elements and do the sort.
从您的描述中,数组结构似乎有点模糊。您可以使用自定义排序函数来比较元素并进行排序。
Assuming the structure is:
假设结构为:
var data = [
[date11, date12, string],
[date21, date22, string],
[date31, date32, string],
...
];
If you had objects instead of nested arrays, you wouldn't need to use number indexes. Here a[0]and b[0]are used to compare the first item in two nested arrays (assuming its the date you want to sort by). Also, assuming that a[0] and b[0] are already objects of Date - you may need to create the Date objects if they aren't already.
如果您有对象而不是嵌套数组,则不需要使用数字索引。这里a[0]和b[0]用于比较两个嵌套数组中的第一项(假设它是您要排序的日期)。此外,假设 a[0] 和 b[0] 已经是 Date 的对象 - 如果尚未创建 Date 对象,您可能需要创建它们。
Update: Thanks to @maerics for pointing this out. The return value from the comparator needs to be [negative, 0, positive] corresponding to [a < b, a == b, a > b] values.
更新:感谢@maerics 指出这一点。比较器的返回值需要是 [negative, 0, positive] 对应于 [a < b, a == b, a > b] 值。
function sortByDate(a, b) {
return a[0].getTime() - b[0].getTime();
}
data.sort(sortByDate);
回答by kennebec
This example is for sorting arrays by numbers (and dates) or strings.
此示例用于按数字(和日期)或字符串对数组进行排序。
Array.prototype.deepsort= function(){
var i, order= arguments, L= order.length, tem;
return this.sort(function(a, b){
i= 0;
while(i < L){
tem= order[i++];
var ao= a[tem] || 0, bo= b[tem] || 0;
if(ao== bo) continue;
return ao> bo? 1: -1;
}
return 0;
});
}
var a= [ [ 'z', 1, 0 ], [ 'a', 0, 1 ],['m',-1,10] ,['a','1',-1]];
alert(a.deepsort(0,1,2)+'\n\n'+a.deepsort(2,0,1))
Sorts on the selected index (passed as an argument).
对选定的索引进行排序(作为参数传递)。
If the items at that index in each array match, sorts on the next index passed as an argument, if any.
如果每个数组中该索引处的项目匹配,则对作为参数传递的下一个索引(如果有)进行排序。
Continue as long as the items match and there are more arguments.
只要项目匹配并且有更多参数就继续。
You don't need to specify more than one index to sort by
您不需要指定多个索引进行排序
a.deepsort(0);
a.deepsort(2);

