【问题标题】:Sort array of object by boolean and cod (number)按布尔值和 cod(数字)对对象数组进行排序
【发布时间】:2021-10-13 09:51:36
【问题描述】:

我有一个对象数组,我正在尝试按类型和代码对它们的信息进行排序。 我的目标是首先获取 type type = true 的行,然后按代码升序排序。

Demo

我试过这个,但他把假的放在最上面,并且没有按升序对代码进行排序。

谁能帮帮我?

.TS

  data = [{
    cod: 1.19,
    type: true
  },
  {
    cod: 1.13,
    type: true
  },
  {
    cod: 1.01,
    type: true
  },
  {
    cod: 1.1,
    type: false
  },
  {
    cod: 1.2,
    type: true
  },
  {
    cod: 2.19,
    type: false
  },
  {
    cod: 3.29,
    type: true
  },
  {
    cod: 5.19,
    type: false
  },
  {
    cod: 1.7,
    type: false
  },]

  ngOnInit(){
    this.data.sort(function (x, y) {
      return (x.type === y.type) ? 0 : x ? -1 : 1;;
  });

  console.log(this.data)
  }

【问题讨论】:

标签: javascript angular typescript sorting


【解决方案1】:

而不是从一行 if 语句开始。我更喜欢使用更多的代码行来更容易理解必须执行的逻辑

// return 1 if x has more value/larger than y
// return -1 if x has less value/smaller than y
data.sort(function (x, y) {
  // type has priority over cod. So cod can be ignored if type is different for x and y
  if (x.type == true && y.type == false) {
    return 1;
  }
  if (x.type == false && y.type == true) {
    return -1;
  }
  // If type is the same for y and x, compare the cod value instead!
  if (x.type == y.type) {
    if (x.cod > y.cod) {
      return 1;
    }
    if (x.cod < y.cod) {
      return -1;
    }
  }
  // Return 0 if they have the exact same
  return 0;
});

改为通过减去 cod 数来减少代码。负数表示 y.cod 较大,正数表示 x.cod 较大。零表示它们相等。

data.sort(function (x, y) {
  if (x.type == true && y.type == false) {
    return 1;
  }
  if (x.type == false && y.type == true) {
    return -1;
  }
  // If type is the same for y and x, compare the cod value instead!
  if (x.type == y.type) {
      // Can be condensed to a single subtraction
      return x.cod - y.cod;
  }
});

可以通过更少的比较进一步改进

data.sort(function (x, y) {
  // these are equivalent to the first two if-statements in the code blocks above.
  if (x.type && !y.type) return 1;
  if (!x.type && y.type) return -1;
  // if none of above has been triggered, the types are equal and cod should be compared.
  return x.cod - y.cod;
});

正如@Eliseo 建议的那样,它可以用三元运算符压缩,这将减少执行时间。

data.sort(function (x,y) {
  return !x.type && y.type ? 1 : (x.type && !y.type ? -1 : x.cod-y.cod);
});

为了理解,从最直接的逻辑开始会更容易,逻辑可以很容易地遵循。然后从那里看看可以做些什么来简化它。

【讨论】:

  • 这已经在副本中得到了回答。如果您觉得它增加了一些东西,请随时将您的答案添加到副本中。
  • data.sort((x,y)=&gt;!x.type &amp;&amp;y.type?1:x.type &amp;&amp; !y.type?-1:x.cod-y.cod)
  • @Eliseo 我看不出有任何理由将其复杂化为一行。一目了然地理解正在发生的事情变得更加困难。
  • 它更快,但确实更难理解:(
  • 我只是比较了一下,确实更快。我会用像你这样的第三个版本来更新答案。至少在我实施了较长的版本后,我发现你的更容易掌握。
【解决方案2】:

我更喜欢这样的:

var sorted = data
    .sort(function(x, y) { 
        return x.cod - y.cod;
    })
    .sort(function(x, y) {
        return y.type - x.type;
    });

编辑:更好的代码格式。

【讨论】:

  • 这已经在副本中得到了回答。如果您觉得它增加了一些东西,请随时将您的答案添加到副本中。
  • 哦,我很抱歉。无论如何,我不会删除这个答案,因为我认为这是做这些事情的最干净的方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-28
  • 2021-07-01
  • 1970-01-01
  • 2021-06-21
  • 2023-01-25
  • 1970-01-01
相关资源
最近更新 更多