【问题标题】:Sorting an Array of comma separated string using JavaScript使用 JavaScript 对逗号分隔的字符串数组进行排序
【发布时间】:2018-07-14 15:14:55
【问题描述】:

我遇到了一个奇怪的要求,我在过去的几个小时里都在努力完成它。下面是我的字符串数组(只是一个例子,实际数组包含大约 2500 条记录):

var testArray = [
  "130,839.9,855,837.3,848.65,3980489", 
  "129,875,875,828.1,833.25,6926078", 
  "138,891.3,893.3,865.2,868.75,5035618"
]

我们这里有 3 个元素,其中每个元素是 comma 分隔的(每个元素有 6 个项目)。即:

testArray[0] = "130,839.9,855,837.3,848.65,3980489"

我的问题是,我想根据每个元素的第一项对testArray 进行排序,并将其转换为具有所有值的数组数组,因此输出将是:

[
  [129, 875, 875, 828.1, 833.25, 6926078],
  [130, 839.9, 855, 837.3, 848.65, 3980489],
  [138, 891.3, 893.3, 865.2, 868.75, 5035618]
]

我能够对单个项目进行排序,但不能将整个数组作为一个整体进行排序,我尝试使用 split 然后排序,但没有成功。

谁能帮我解决这个问题,如果我不清楚,请告诉我。

【问题讨论】:

  • 最好也展示一下尝试。

标签: javascript arrays sorting split array-map


【解决方案1】:

Array#map 中使用Array#map 转换数组,然后根据[0] 索引(a[0] - b[0]) 对转换后的数组使用Array#sort

在 ES5 中

var testArray = [
  "130,839.9,855,837.3,848.65,3980489", 
  "129,875,875,828.1,833.25,6926078", 
  "138,891.3,893.3,865.2,868.75,5035618"
]

var converted = testArray.map(function (item) {
  return item.split(',').map(function (num) {
    return parseFloat(num);
  });
})
console.log(converted)

var sorted = converted.sort(function (a, b) { return a[0] - b[0] })
console.log(sorted)

在 ES6 中

const testArray = [
  "130,839.9,855,837.3,848.65,3980489", 
  "129,875,875,828.1,833.25,6926078", 
  "138,891.3,893.3,865.2,868.75,5035618"
]

const converted = testArray.map(
  item => item.split(',').map(
    num => parseFloat(num)
  )
)
console.log(converted)

const sorted = converted.sort((a, b) => a[0] - b[0])
console.log(sorted)

在 ES6 中(精简)

const testArray = [
  "130,839.9,855,837.3,848.65,3980489", 
  "129,875,875,828.1,833.25,6926078", 
  "138,891.3,893.3,865.2,868.75,5035618"
]

const convertedAndSorted = testArray
  .map(n => n.split(',')
  .map(num => parseFloat(num)))
  .sort((a, b) => a[0] - b[0])

console.log(convertedAndSorted)

【讨论】:

  • ES5 代码中有一个错字:return item.split('.') 应该是return item.split(',')
【解决方案2】:

只需将拆分后的和数字格式的值映射并按第一项排序。

var data = ["130,839.9,855,837.3,848.65,3980489", "129,875,875,828.1,833.25,6926078", "138,891.3,893.3,865.2,868.75,5035618"],
    result = data
        .map(s => s.split(',').map(Number))
        .sort((a, b) => a[0] - b[0]);
        
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

【讨论】:

    【解决方案3】:
    var testArray = ["130,839.9,855,837.3,848.65,3980489","129,875,875,828.1,833.25,6926078","138,891.3,893.3,865.2,868.75,5035618"];
    
    const output = [];
    for (let i = 0; i < testArray.length; i++) {
        var numbers = testArray[i].split(',');
        for (let j = 0; j < numbers.length; j++) {
            numbers[j] = +numbers[j];
        }
        output[i] = numbers;
    }
    output.sort(function(x, y) {
        return x[0] - y[0];
    });
    

    或更短

    output = testArray.map(s => s.split(',')).map(e => e.map(n => +n)).sort((x, y) => x[0] - y[0]);
    

    【讨论】:

      【解决方案4】:

      首先使用Array.map()parseFloat() 将每个字符串转换为浮点值数组。 之后,您可以使用 Arrays.sort() 对数组数组进行简单的排序

      尝试以下方法:

      var arr = ["130,839.9,855,837.3,848.65,3980489","129,875,875,828.1,833.25,6926078","138,891.3,893.3,865.2,868.75,5035618"];
      
      var result = arr.map((a)=> a.split(",").map((b)=>parseFloat(b))).sort((a,b)=> a[0] -b[0]);
      console.log(result);

      【讨论】:

      • 您将浮点数排序为字符串。
      • @ASDFGerte 感谢您指出,我在编辑问题时打错字了。
      • 顺便说一句,在编辑之前,您的排序甚至首先将子数组强制转换为字符串,然后将它们排序为字符串,这有点太苛刻了。
      猜你喜欢
      • 2020-03-14
      • 2017-07-17
      • 2016-01-16
      • 2022-01-01
      • 2020-12-04
      • 2016-01-18
      • 2021-12-06
      • 2022-11-03
      • 1970-01-01
      相关资源
      最近更新 更多