如何根据字符串中的数字对数组进行排序?

时间:2017-04-16 00:40:38

标签: javascript arrays regex sorting

鉴于此数组= [ "3ab1", "2a0", "1abc2" ]

如何将其排序为[ "1abc2", "3ab1", "2a0" ](最后一个数字的降序)

并返回[ 1,3,2 ]。 (每个学期的第一个数字)

当最后一个数字和下一个最后一个数字不连续时,返回的值应为0.

[ "2x2", "3x0", "2x1" ] ==> [ 2, 2, 3 ]

[ "22x0", "3x9", "2x1" ] ==> [ 3, 0, 0, 0, 0, 0, 0, 0, 2, 22 ]

[ "2x4", "3x0" ] ==> [ 2, 0, 0, 3 ]

[ "axn", "bx(n-2)" ] ==> [ "axn", "0x(n-1)", bx(n-2) ] ==> [ a, 0, b ]

我正在考虑将数组转换为字符串,替换前面的数字和字母,然后对数组进行排序。但我不知道如何将被替换的部分放回原来的数字。这是我尝试在最终数组排序后返回它。

var ary = [ "1abc2", "3ab1", "2a0" ];

console.log(((ary.toString()).match(/\d+(?!,)/g)).slice(0, -1));

我在基于数字排序数组时看到了这些问题,但它们似乎对我不起作用。

How to sort an array of integers correctly

Sort Array Elements (string with numbers), natural sort

4 个答案:

答案 0 :(得分:0)

您的问题有点奇怪,但您可以使用mapparseInt实现此目的:



var arr = [ "1abc2", "3ab1", "2a0" ];

var res = arr.map(function (i) {
  return parseInt(i);
});

console.log(res);




答案 1 :(得分:0)

排序和地图的组合应该可以解决问题。

  const ary = [ "1abc2", "3ab1", "2a0" ];
  const newarray = ary
      .sort((a, b) => {
        return a[a.length - 1] < b[b.length - 1];
      })
      .map((a) =>  {
        return parseInt(a[0]);
      });
  console.log(newarray);

答案 2 :(得分:0)

下面的脚本首先根据结束号对数组进行降序排序 然后只返回已排序数组的起始编号。

(我更改了数组中的数字,表明它们可以超过一位数。)

rootProject.name = root

include ':p1'
include ':p2'
include ':p3'    // Keep this if this folder contains a build.gradle
include ':p3:sp1'
include ':p3:sp2'
jsfiddle:https://jsfiddle.net/nu8vf837/

答案 3 :(得分:0)

使用sortreduce获取结果时,您可以使用正则表达式来获取数字:

&#13;
&#13;
var array = [ "22x0", "3x9", "2x1" ];

var reS = /^\d+/,                          // regexp for getting all digits at the start of the string
    reE = /\d+$/;                          // regexp for getting all digits at the end of the string
var result = array.sort(function(a, b) {   // First: sort the array
    a = reE.exec(a);                       // get the last number from the string a
    b = reE.exec(b);                       // get the last number from the string b
    return b - a;                          // sort in a descending order
}).reduce(function(res, str, i) {          // Then: accumulate the result array 
    var gap = reE.exec(array[i - 1]) - reE.exec(str); // calculate the gap between this string str and the last string array[i - 1] (gap = N_of_last_string - N_of_this_string)
    if(gap > 0)                            // if there is a gap
        while(--gap) res.push(0);          // then fill it with 0s
    res.push(+reS.exec(str));              // push this string number
    return res;
}, []);

console.log("Sorted array:", array);       // array is now sorted
console.log("Result:", result);            // result contain the numbers
&#13;
&#13;
&#13;

在最近的ECMAScript版本中,您可以使用箭头函数很快完成此操作:

&#13;
&#13;
let array = [ "22x0", "3x9", "2x1" ];

let reS = /^\d+/,                       
    reE = /\d+$/;                      
let result = array.sort((a, b) => reE.exec(b) - reE.exec(a))
                  .reduce((res, str, i) => { 
                      let gap = reE.exec(array[i - 1]) - reE.exec(str);
                      if(gap > 0)
                          while(--gap) res.push(0);
                      res.push(+reS.exec(str));
                      return res;
                  }, []);

console.log("Sorted array:", array); 
console.log("Result:", result);
&#13;
&#13;
&#13;