sort outer array based on values in inner array, javascript

后端 未结 3 1998
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-29 10:02

I have an array with arrays in it, where I want to sort the outer arrays based on values in a specific column in the inner.

I bet that sounded more than a bit confus

3条回答
  •  清歌不尽
    2020-11-29 10:18

    Here is a solution not needing a separate variable to contain the index

    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

提交回复
热议问题