排序在JavaScript多维数组 [英] Sorting a multidimensional array in javascript

查看:134
本文介绍了排序在JavaScript多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你会

如何排序在JavaScript中多维数组?

我有一个数组完全包含两个日期和一个字符串数组。我需要通过之日起阵列中的一个排序的主阵列,这可能吗?

数据stucture:

 事件= [
 {1日期,日期2,串},
 {2日,日期2,串},
 ]


解决方案

的<一个重复href=\"http://stackoverflow.com/questions/2793847/sort-outer-array-based-on-values-in-inner-array-javascript\">http://stackoverflow.com/questions/2793847/sort-outer-array-based-on-values-in-inner-array-javascript
在这里,你会发现几个答案,就像我自己

  VAR ARR = [......]
arr.sort((函数(指数){
    返回功能(A,B){
        回报(A [指数] === B〔指数] 0:?(A [指数]&LT; B [指数] -1:1));
    };
})(2)); // 2是索引

这对排序索引2

How would you sort a multidimensional array in 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 },
 ]

解决方案

Duplicate of http://stackoverflow.com/questions/2793847/sort-outer-array-based-on-values-in-inner-array-javascript here you will find several answers, like my own

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

这篇关于排序在JavaScript多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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