【问题标题】:Sorting Array of Strings by Character in Alphabetical or Numerical按字母或数字的字符对字符串数组进行排序
【发布时间】:2018-10-08 19:45:43
【问题描述】:

所以我有一个要排序的字符串数组。

假设我有这个数组:

let array = ["8AD", "8AB", "8A6", "8BC", "86F", "835", "81D"];

现在,需要实现两种排序:

  1. 字母字符优先于数字字符
  2. 数字字符优先于字母字符

现在,我需要对每个字符使用这两个中的任何一个进行排序。

所以,数字-数字-字母顺序会给我:

"81D","835","86F","8AB","8AD","8A6","8BC"

虽然数字-字母-数字顺序会给我:

"8A6","8AB","8AD","8BC","81D","835","86F"

我正在考虑将每个数字和所有字符分配给一个两位整数:

let alpha = {
    A= 11, B= 12 , C= 13 , D= 14 , E= 15 , F= 16 , G= 17 , H= 18 , I= 19 , J= 20 , K= 21,
    L= 22 , M= 23 , N= 24 , O= 25 , P= 26 , Q= 27 , R= 28 , S= 29 , T= 30 , U= 31 , V= 32,
    W= 33 , X= 34 , Y= 35 , Z= 36 , 0=37, 1= 38 , 2= 39 , 3= 40 , 4= 41 , 5= 42 , 6= 43 , 7= 44,
    8= 45 , 9= 46 };
let numeral = {
    0=11, 1=12, 2=13, 3=14, 4=15, 5=16, 6=17, 7=18, 8=19, 9=20, A=21, 
    B=22, C=23, D=24, E=25, F=26, G=27, H=28, I=29, J=30, K=31, L=32, 
    M=33, N=34, O=35, P=36, Q=37, R=38, S=39, T=40, U=41, V=42, W=43, 
    X=44, Y=45, Z=46 }

然后将每个字符替换为所需的任何顺序。 有没有人有更简单或更有效的方法来实现需要做的事情?

【问题讨论】:

    标签: javascript sorting


    【解决方案1】:

    这里有一些通用/抽象的东西

    // given a sequence and an array of "order" strings, create a comparable "key" string
    
    let multiSortKey = (subject, orders) =>
        [...subject].map(
            (c, i) =>
                String(orders[i].indexOf(c)).padStart(16, '0')
        ).join();
    
    
    // generic comparison function
    
    let cmp = (a, b) => (a > b) - (a < b);
    
    // generic sort-by-map, aka Schwartzian, function
    
    let sortBy = (xs, key) => xs
        .map(x => [x, key(x)])
        .sort((x, y) => cmp(x[1], y[1]))
        .map(x => x[0]);
    
    
    // applied to the task at hand:
    
    data = ["8AD", "8AB", "8A6", "8BC", "86F", "835", "81D"]
    
    N = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    A = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
    
    console.log(sortBy(data, x => multiSortKey(x, [N, N, A])))
    console.log(sortBy(data, x => multiSortKey(x, [N, A, N])))

    【讨论】:

      【解决方案2】:

      尝试使用带有radix 参数的use parseInt() 函数将您的字符串转换为数值。

      也可以.sort()method 对您的数组进行排序。

      示例:

      let array = ["8AB", "8A6", "8BC", "86F", "835", "81D", "22", "33", "0"];
      array.sort((x,y) => parseInt(x, 32) - parseInt(y, 32));
      console.log(array);

      【讨论】:

        【解决方案3】:

        查看每个字母和sorting with map 的模式的方法。

        function sort(array, pattern) {
            return array
                .map((value, index) => ({ index, value: Array.from(value, c => pattern.map(fn => fn(c) ? c : ' ').join('')).join('') }))
                .sort(({ value: a }, { value: b }) => a.localeCompare(b))
                .map(({ index }) => array[index]);
        }
        
        const
            isDigit = c => /^\d$/.test(c),
            isNonDigit = c => /^\D$/.test(c),
            digitNonDigit = [isNonDigit, isDigit], // revers sorted
            nondigitDigit = [isDigit, isNonDigit], // revers sorted
            array = ["8AD", "8AB", "8A6", "8BC", "86F", "835", "81D"];
        
        console.log(sort(array, digitNonDigit));
        console.log(sort(array, nondigitDigit));
        .as-console-wrapper { max-height: 100% !important; top: 0; }

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-03-10
          • 1970-01-01
          • 1970-01-01
          • 2013-06-11
          • 1970-01-01
          • 2021-07-13
          • 2018-04-21
          • 1970-01-01
          相关资源
          最近更新 更多