简体   繁体   English

基于内部数组javascript中的值对外部数组进行排序

[英]sort outer array based on values in inner array, javascript

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 confusing, so I'll skip straight to an example. 我打赌这听起来有点混乱,所以我会直接跳到一个例子。

Initial data: 初步数据:

var data = [
  [
    "row_1-col1",
    "2-row_1-col2",
    "c-row_1-coln"
  ],
  [
    "row_2-col1",
    "1-row_2-col2",
    "b-row_2-coln"
  ],
  [
    "row_m-col1",
    "3-row_m-col2",
    "a-row_m-coln"
  ]
];

Sort data, based on column with index 1 根据索引为1的列对数据进行排序

data.sortFuncOfSomeKind(1);

where the object then would look like this; 然后对象看起来像这样;

var data = [
  [
    "row_2-col1",
    "1-row_2-col2",
    "b-row_2-coln"
  ],
  [
    "row_1-col1",
    "2-row_1-col2",
    "c-row_1-coln"
  ],
  [
    "row_m-col1",
    "3-row_m-col2",
    "a-row_m-coln"
  ]
];

Sort data, based on column with index 2 根据索引为2的列对数据进行排序

data.sortFuncOfSomeKind(2);

where the object then would look like this; 然后对象看起来像这样;

var data = [
  [
    "row_m-col1",
    "3-row_m-col2",
    "a-row_m-coln"
  ],
  [
    "row_2-col1",
    "1-row_2-col2",
    "b-row_2-coln"
  ],
  [
    "row_1-col1",
    "2-row_1-col2",
    "c-row_1-coln"
  ]
];

The big Q 大Q.

Is there an existing solution to this that you know of, or would I have to write one myself? 你知道吗,或者我必须自己写一个解决方案吗? If so, which would be the easiest sort algorithm to use? 如果是这样,哪个是最容易使用的排序算法? QuickSort? 快速排序?

_L _L

Array#sort (see section 15.4.4.11 of the spec , or MDC ) accepts an optional function parameter which will be used to compare two entries for sorting purposes. Array#sort (参见规范的第15.4.4.11节,或MDC )接受一个可选的函数参数,该参数将用于比较两个条目以进行排序。 The function should return -1 if the first argument is "less than" the second, 0 if they're equal, or 1 if the first is "greater than" the second. 如果第一个参数是“小于”第二个参数,则函数应该返回-1,如果它们相等则返回0,如果第一个参数是“大于”第二个参数,则返回1。 So: 所以:

outerArray.sort(function(a, b) {
    var valueA, valueB;

    valueA = a[1]; // Where 1 is your index, from your example
    valueB = b[1];
    if (valueA < valueB) {
        return -1;
    }
    else if (valueA > valueB) {
        return 1;
    }
    return 0;
});

(You can obviously compress that code a bit; I've kept it verbose for clarity.) (你显然可以稍微压缩一下这些代码;为了清楚起见,我保持冗长。)

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 这在索引2上排序

Here used to be a sort implementation that returned the result of a simple x<y comparison. 这里曾经是一个返回简单x<y比较结果的排序实现。 This solution is disencouraged and this post is left only to preserve the ensuing discussion. 这个解决方案是不鼓励的,这个帖子只是为了保留随后的讨论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM