【问题标题】:Sort array having numbers in "A/B" format?对具有“A/B”格式数字的数组进行排序?
【发布时间】:2018-03-30 11:19:24
【问题描述】:

我得到了一个类似 :arr = ["1/8", "2/8", "1/17", "4/17", "3/5", "1/2"] 的数组 现在我需要得到一个像

这样的排序数组
[
"1/17",
"4/17",
"1/8",
"2/8",
"3/5",
"1/2"
]

那么有人有什么好的建议吗?提前致谢!

PS:这只是一个例子,可能数据是其他的,但都是A/B格式。

【问题讨论】:

  • 你想要js还是java的解决方案?
  • 你必须告诉我们,你是如何排序的。按斜线后面的数字?
  • @AniruddhAgarwal 任何都可以。更好的 js ;)
  • 我假设您想按值排序,因此您可以使用 ScriptEngineManager 与基本 Comparator 一起进行评估。
  • 排序的逻辑是什么?

标签: javascript java arrays sorting


【解决方案1】:

在js中可以使用sort

var arr = ["1/8", "2/8", "1/17", "4/17", "3/5", "1/2"];

arr.sort((a, b) => {
  let aParts = a.split("/");
  let bParts = b.split("/");

  //Given value A/B

  if (aParts[1] === bParts[1]) return aParts[0] - bParts[0]; //Check if B is the same, if it is use A to sort
  return bParts[1] - aParts[1];                              //B is not the same, so sort using b
});

console.log(arr);

短版:

var arr = ["1/8", "2/8", "1/17", "4/17", "3/5", "1/2"];

arr.sort((a, b) => {
  let x = a.split("/"),y = b.split("/");
  return x[1] === y[1] ? x[0] - y[0] : y[1] - x[1];
});

console.log(arr);

【讨论】:

    【解决方案2】:

    您可以使用Array.sort 方法进行排序,并使用split() 方法对/ 之后的第二个元素进行排序。

    在 ES5 中

    var result = ["1/8", "2/8", "1/17", "4/17", "3/5", "1/2", '1/2'].sort(function(a, b) {
      if (a == b) {
        console.log('value same', a, b);
        return 0
      }
      a = parseInt(a.split('/')[1]);
      b = parseInt(b.split('/')[1]);
      return a > b ? -1 : (a < b ? 1 : 0);
    });
    
    console.log(result)

    在 ES6 中

    let result= ["1/8", "2/8", "1/17", "4/17", "3/5", "1/2",'2/8'].sort((a, b) => {
     if (a == b) {
        console.log('value same',a,b);
        return 0
      }
      a = +a.split('/')[1];
      b = +b.split('/')[1];
      return a > b ? -1 : (a < b ? 1 : 0);
    });
    
    console.log(result)

    【讨论】:

    • 不,这不行,因为不能保证使用最后一部分进行排序。
    【解决方案3】:

    arr = [“1/8”、“2/8”、“1/17”、“4/17”、“3/5”、“1/2”]

    创建一个 hashMap 或任何可以保存键值对的数据结构(假设名称是 arrMap)

    创建一个可以保存浮点值的数组(假设名称是 tempArray)

    loop: arr elements
    
    1st element is 1/8
    
        Use substring method to get before slash and after slash values
        Hence u will get
          Var beforeSlash = 1 // in string format
    
          Var afterSlash = 8     // in string format
    
          These are in string hence Do parseInt     
    
             Var beforeSlash = 1 // in int format
    
             Var afterSlash = 8  // in int format
    
             Var division =  beforeSlash / afterSlash; // division is in float
    
             Put in arrMap the value division with key “ beforeSlash / afterSlash” 
             [here it will be  key: value is “1/8” : 0.125]
             Put division in tempArray. // tempArray [0.125] 
    end loop
    

    执行后 tempArray 将为 [0.125, 0.25, 0.0588, 0.235, 0.6, 0.5]

    应用任何默认排序方法

    例如。 tempArray.sort()

    最终的 tempArray 将变为 [0.0588, 0.125, 0.235, 0.25, 0.5, 0.6 ]

    执行后 arrMap 将是

    [

    “1/8”:0.125,

    “2/8”:0.25,

    “1/17”:0.0588,

    “4/17”:0.235,

    “3/5”:0.6,

    “1/2”:0.5

    ]

    用 hashMap 中的键替换 tempArray 值

    因此 finalArry 变成 [“1/17”、“1/8”、“4/17”、“2/8”、“1/2”、“3/5”]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-01
      • 2014-08-03
      • 2015-03-05
      • 1970-01-01
      • 1970-01-01
      • 2021-12-11
      相关资源
      最近更新 更多