javascript 按第二个值对二维数组进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3524827/
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
Sort a 2D array by the second value
提问by Noe
I have an array and I want to sort by the number field not the name.
我有一个数组,我想按数字字段而不是名称排序。
var showIt = [
["nuCycleDate",19561100],
["ndCycleDate",19460700],
["neCycleDate",0],
["nlCycleDate",0]
];
Thanks
谢谢
回答by lincolnk
you can provide sortwith a comparison function.
您可以提供sort比较功能。
showIt.sort(function(a,b){
return a[1] - b[1];
});
aand bare items from your array. sort expects a return value that is greater than zero, zero, or less than zero. the first indicates acomes before b, zero means they are equal, the last option means bfirst.
a并且b是您阵列中的项目。sort 期望返回值大于零、零或小于零。第一个表示a在之前b,零表示它们相等,最后一个选项表示b在前。

