按第二个值对2D数组进行排序 [英] Sort a 2D array by the second value

查看:110
本文介绍了按第二个值对2D数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,我想按数字字段排序而不是名字。

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]
];

谢谢

推荐答案

你可以用比较函数提供 sort

you can provide sort with a comparison function.

showIt.sort(function(a,b){
    return a[1] - b[1];
});

a b 是您阵列中的项目。 sort期望返回值大于零,零或小于零。第一个表示 a b 之前,零表示它们相等,最后一个选项表示 b 首先。

a and b are items from your array. sort expects a return value that is greater than zero, zero, or less than zero. the first indicates a comes before b, zero means they are equal, the last option means b first.

这篇关于按第二个值对2D数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆