在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