【问题标题】:How to sort a 2D array by the first column using values of another array?如何使用另一个数组的值按第一列对二维数组进行排序?
【发布时间】:2021-09-13 16:23:15
【问题描述】:

我有一个二维数组:

var array1 = [
["Jasper", "Diesel", "Grid", 2.7],
["Delta", "Solar", "Grid", 10],
["Alpha", "Wind", "Grid", 20]
]

我想使用第一列对array1 进行排序:

var sortingArray = ["Alpha","Jasper","Delta]

所以它应该是这样的:

var sortedArray = [
["Alpha", "Wind", "Grid", 20],    
["Jasper", "Diesel", "Grid", 2.7],
["Delta", "Solar", "Grid", 10]
]

我已经尝试过使用下面的sort 函数,但它根本不会改变数组。我不需要指定要排序的位置吗?

array1.sort(function (a, b) {  
  return sortingArray.indexOf(a) - sortingArray.indexOf(b);
})

更新:自从发布以来,我已经解决了这个问题。正如我所怀疑的,我确实需要提供我想要排序的位置。解决办法是:

array1.sort(function (a, b) {  
  return sortingArray.indexOf(a[0) - sortingArray.indexOf(b[0]);
})

【问题讨论】:

标签: javascript arrays sorting


【解决方案1】:

ab 是数组,第一个元素是字符串。您可以使用localeCompare字符串方法比较字符串:

array1.sort((a, b) => a[0].localeCompare(b[0]))

【讨论】:

    【解决方案2】:
    array1.sort(function (a, b) {  
       return sortingArray.indexOf(a[0) - sortingArray.indexOf(b[0]);
    })
    

    【讨论】:

      【解决方案3】:

      您可以将排序数组缩减为 Map 以便快速查找。

      const array1 = [
        ["Jasper", "Diesel", "Grid", 2.7],
        ["Delta", "Solar", "Grid", 10],
        ["Alpha", "Wind", "Grid", 20]
      ]
      
      const sortingArray = ["Alpha", "Jasper", "Delta"];
      
      /**
       * Sorts the data by a list of values.
       * @param {Object[]|Array[]} data - an array of objects or arrays
       * @param {String|Number} fieldOrIndex - a field name or index
       * @param {String[]} order - the sorting order of the values
       * @return {Object[]|Array[]} sorted data
       */
      const sortWith = (data, fieldOrIndex, order) => {
        const orderMap = order.reduce((acc, token, index) =>
          acc.set(token, index), new Map);
        return data.sort((a, b) =>
          orderMap.get(a[fieldOrIndex]) - orderMap.get(b[fieldOrIndex]));
      };
      
      const sorted = sortWith(array1, 0, sortingArray);
      
      console.log(sorted);
      .as-console-wrapper { top: 0; max-height: 100% !important; }

      如果您的列表或项目很小,您可以跳过地图,直接抓取索引。

      const array1 = [
        ["Jasper", "Diesel", "Grid", 2.7],
        ["Delta", "Solar", "Grid", 10],
        ["Alpha", "Wind", "Grid", 20]
      ]
      
      const sortingArray = ["Alpha", "Jasper", "Delta"];
      
      /**
       * Sorts the data by a list of values.
       * @param {Object[]|Array[]} data - an array of objects or arrays
       * @param {String|Number} fieldOrIndex - a field name or index
       * @param {String[]} order - the sorting order of the values
       * @return {Object[]|Array[]} sorted data
       */
      const sortWith = (data, fieldOrIndex, order) =>
        data.sort((a, b) =>
          order.indexOf(a[fieldOrIndex]) - order.indexOf(b[fieldOrIndex]));
      
      const sorted = sortWith(array1, 0, sortingArray);
      
      console.log(sorted);
      .as-console-wrapper { top: 0; max-height: 100% !important; }

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-03
      • 2011-10-25
      • 2015-08-25
      • 1970-01-01
      • 1970-01-01
      • 2012-04-03
      • 2013-04-12
      相关资源
      最近更新 更多