简体   繁体   English

使用JavaScript对以逗号分隔的字符串数组进行排序

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

I came across with a weird requirement and I am struggling for last few hours to complete it. 我遇到了一个奇怪的要求,而我在最后几个小时内都在努力地完成它。 Below is my Array of string(just an example, the actual array contains around 2500 records): 以下是我的字符串数组(仅作为示例,实际数组包含大约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"
]

We have 3 element here of which each element is comma separated(each element have 6 item). 这里有3个元素,每个元素之间用comma分隔(每个元素有6个项目)。 ie: 即:

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

My problem is, I wanted to sort testArray based on the first item of each element and convert it to array of array having all value into float, so the output would be: 我的问题是,我想根据每个元素的第一项对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]
]

I am able to sort individual item but not the entire array as a whole, and I have tried using split and then sort with no luck. 我能够对单个项目进行排序,但不能对整个数组进行整体排序,并且我尝试使用split进行排序,但是运气不好。

Can someone help me out with this and please let me know if I am not clear. 有人可以帮我解决这个问题,如果我不清楚的话,请告诉我。

Convert the array using Array#map within an Array#map , then use Array#sort on the converted array according to the [0] indices ( a[0] - b[0] ): 使用Array#map中的Array#map转换数组,然后根据[0]索引( a[0] - b[0] )在转换后的数组上使用Array#sort

In ES5 在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) 

In ES6 在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) 

In ES6 (condensed) 在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) 

First convert each of the Strings to an array of floats values using Array.map() and parseFloat() . 首先使用Array.map()parseFloat()将每个String转换为float值数组。 After that you can simply sort the array of arrays using Arrays.sort() 之后,您可以简单地使用Arrays.sort()对数组数组进行Arrays.sort()

Try the following : 尝试以下方法:

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

Just map the splitted and to number formatted values and sort by the first item. 只需将拆分后的值映射到数字格式的值,然后按第一项排序即可。

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

or shorter 或更短

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.

相关问题 为什么用javaScript中的split()将逗号分隔的字符串不能转换为数组? - Why is the comma-separated string not converting to an array using split() in javaScript? 逗号分隔的字符串成多个 arrays 用逗号分隔,使用 javascript - Comma separated string into multiple arrays separated by comma using javascript 如何在 JavaScript 中将逗号分隔的字符串转换为数组 - How to Convert Comma Separated String into an Array in JavaScript 逗号分隔数字的字符串到javascript中的整数数组 - String with Comma separated numbers to array of integer in javascript Javascript字符串数组到多个用逗号分隔的字符串 - Javascript string array to multiple strings separated by a comma 将逗号分隔的字符串转换为 JavaScript 数组 - Convert comma separated string to a JavaScript array 使用JavaScript解析逗号分隔的字符串? - Parse comma separated string using JavaScript? 用逗号分隔的Javascript字符串 - Javascript string separated by a comma 如何在不使用.join()的情况下将数组转换为不带逗号的字符串并在javascript中用空格分隔? - How to convert array into string without comma and separated by space in javascript without using .join()? 如何使用 JavaScript 和 NodeJS 避免数组中的空对象与用逗号分隔的字符串? - How to avoid empty objects in array from string separated by comma using JavaScript and NodeJS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM