Javascript:多维数组排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6993302/
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
Javascript: Sort Multidimensional Array
提问by Joe Yan
I have a Multidimensional Array which has 3 columns (by using javascript)
我有一个多维数组,它有 3 列(通过使用 javascript)
[0] Number of vote
[1] Name of candidate
[2] Candidate Number
My array contents are:
我的数组内容是:
1 | Peter | 3
1 | Mary | 2
0 | David | 5
0 | John | 4
0 | Billy | 1
How can I sort the array by [0] Number of voteand then [2] candidate number?
如何按 [0]票数然后按 [2]候选人编号对数组进行排序?
The result should be:
结果应该是:
1 | Mary | 2
1 | Peter | 3
0 | Billy | 1
0 | John | 4
0 | David | 5
回答by Bojan Bjelic
As previously said, you should use a custom sort function. Here's one that would do exactly what you want.
如前所述,您应该使用自定义排序函数。这是一个可以完全按照您的要求执行的操作。
var arr = [];
arr[0] = [1, 'Peter', 3];
arr[1] = [1, 'Mary', 2];
arr[2] = [0, 'David', 5];
arr[3] = [0, 'John', 4];
arr[4] = [0, 'Billy', 1];
arr.sort(function (a,b) {
if (a[0] < b[0]) return 1;
if (a[0] > b[0]) return -1;
if (a[2] > b[2]) return 1;
if (a[2] < b[2]) return -1;
return 0;
});
回答by Shock
array.sort( function (a,b) {
if (a[0] > b[0]) return 1;
if (a[0] < b[0]) return -1;
if (a[2] > b[2]) return 1;
if (a[2] < b[2]) return -1;
return 0;
});
回答by Shadow
As far as I know you will have to make your own sorting function.
If you can store it in a object than defining a function like in the previous answer will do the job.
Refer Sort array of objects
据我所知,您将必须制作自己的排序功能。
如果您可以将它存储在一个对象中,而不是像前面的答案那样定义一个函数就可以完成这项工作。
请参阅排序对象数组
回答by Nilesh
Here is a generic function
这是一个通用函数
function arraySort(pArray)
{
pArray.sort(
function(a,b)
{
var len=a.length;
for (var i=0;i<len;i++)
{
if (a[i]>b[i]) return 1;
else if (a[i]<b[i]) return -1;
}
return 0;
}
);
}
回答by JMP
Another method is to create a value for each array entry, e.g. 1000*number of votes + candidate number, so that this value is unambiguous and unique, e.g. we get 1003, 1002, 5, 4, 1. Add a key refering back to the original array and sort.
另一种方法是为每个数组条目创建一个值,例如 1000*投票数 + 候选人数,以便该值明确且唯一,例如我们得到 1003, 1002, 5, 4, 1。添加一个引用回的键原始数组并排序。
So we would sort [[1003,0],[1002,1],[5,2],[4,3],[1,4]] by the first element of each subarray.
所以我们将按每个子数组的第一个元素对 [[1003,0],[1002,1],[5,2],[4,3],[1,4]] 进行排序。
There is a discrepancy in your sorting system, you use high->low for votes and low-high for candidate number.
您的排序系统存在差异,您使用高->低选票,低-高选票。
回答by user2314737
To sort a multi-dimensional array comparing by coordinate 0 first and then by component 2 you can combine two compare functions with ||
:
要首先按坐标 0 比较然后按组件 2 对多维数组进行排序,您可以将两个比较函数与 结合使用||
:
compare 0-coordinates
||
compare 2-coordinates
比较 0 坐标
||
比较 2 坐标
For numeric values simply use a difference as a compare function, as x - y
is:
对于数值,只需将差值用作比较函数,如下所示x - y
:
- 0 if
x == y
- >0 if
x > y
- <0 if
x < y
- 0 如果
x == y
- >0 如果
x > y
- <0 如果
x < y
Adjust the order of the elements in the differences to take care of ascending/descending order.
调整差异中元素的顺序以处理升序/降序。
var myArray = [
[1, 'Peter', 3],
[1, 'Mary', 2],
[0, 'David', 5],
[0, 'John', 4],
[0, 'Billy', 1]
];
myArray.sort(function(a, b) {
return b[0] - a[0] || a[2] - b[2];
});
console.log(JSON.stringify(myArray));