最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Sorting an Array of comma separated string using JavaScript - Stack Overflow

programmeradmin2浏览0评论

I came across with a weird requirement and I am struggling for last few hours to plete it. Below is my Array of string(just an example, the actual array contains around 2500 records):

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 ma separated(each element have 6 item). i.e:

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:

[
  [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.

Can someone help me out with this and please let me know if I am not clear.

I came across with a weird requirement and I am struggling for last few hours to plete it. Below is my Array of string(just an example, the actual array contains around 2500 records):

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 ma separated(each element have 6 item). i.e:

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:

[
  [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.

Can someone help me out with this and please let me know if I am not clear.

Share Improve this question edited Jul 14, 2018 at 16:43 Rick 4,1249 gold badges27 silver badges37 bronze badges asked Jul 14, 2018 at 15:14 Shail_beeShail_bee 5096 silver badges24 bronze badges 1
  • It's always better to show the attempts aswell. – ASDFGerte Commented Jul 14, 2018 at 15:18
Add a ment  | 

4 Answers 4

Reset to default 8

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

In 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

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)

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)

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

First convert each of the Strings to an array of floats values using Array.map() and parseFloat(). After that you can simply sort the array of arrays using 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);

发布评论

评论列表(0)

  1. 暂无评论