簡體   English   中英

使用JavaScript對以逗號分隔的字符串數組進行排序

[英]Sorting an Array of comma separated string using JavaScript

我遇到了一個奇怪的要求,而我在最后幾個小時內都在努力地完成它。 以下是我的字符串數組(僅作為示例,實際數組包含大約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進行排序,然后將其轉換為具有所有值的數組array,使其變為float,因此輸出為:

[
  [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進行排序,但是運氣不好。

有人可以幫我解決這個問題,如果我不清楚的話,請告訴我。

使用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) 

首先使用Array.map()parseFloat()將每個String轉換為float值數組。 之后,您可以簡單地使用Arrays.sort()對數組數組進行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); 

只需將拆分后的值映射到數字格式的值,然后按第一項排序即可。

 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; } 

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]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM