在javascript中对多维数组进行排序 [英] Sorting a multidimensional array in javascript

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

问题描述

你会如何在 JavaScript 中对多维数组进行排序?

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?

数据结构:

events = [
 { date 1, date 2, string },
 { date 2, date 2, string },
 ]

推荐答案

重复的 根据内部数组中的值对外部数组进行排序,javascript在这里你会找到几个答案,就像我自己的

Duplicate of 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

这对索引 2 进行排序

This sorts on index 2

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

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