【问题标题】:Sorting array by property value bug按属性值排序数组错误
【发布时间】:2020-02-04 17:32:09
【问题描述】:

我正在尝试按属性 commonVotes 以降序方式对数组进行排序。除了排序后的最后一个元素(Pride)没有放在正确的位置之外,排序进行得还不错。

排序前的数组:

[
    {
        "uuid": "4bf84d40-476e-11ea-87f1-f1c0fe90cd16",
        "name": "Pride",
        "createdAt": "2020-02-04T16:49:23.475Z",
        "updatedAt": "2020-02-04T16:49:23.475Z",
        "commonVotes": 70,
        "deadlyVotes": 76
    },
    {
        "uuid": "4bf84d41-476e-11ea-87f1-f1c0fe90cd16",
        "name": "Lust",
        "createdAt": "2020-02-04T16:49:23.475Z",
        "updatedAt": "2020-02-04T16:49:23.475Z",
        "commonVotes": 17,
        "deadlyVotes": 105
    },
    {
        "uuid": "4bf84d42-476e-11ea-87f1-f1c0fe90cd16",
        "name": "Glutony",
        "createdAt": "2020-02-04T16:49:23.475Z",
        "updatedAt": "2020-02-04T16:49:23.475Z",
        "commonVotes": 40,
        "deadlyVotes": 21
    },
    {
        "uuid": "4bf84d43-476e-11ea-87f1-f1c0fe90cd16",
        "name": "Envy",
        "createdAt": "2020-02-04T16:49:23.475Z",
        "updatedAt": "2020-02-04T16:49:23.475Z",
        "commonVotes": 82,
        "deadlyVotes": 75
    }
]

排序后的数组:

[
    {
        "uuid": "4bf84d43-476e-11ea-87f1-f1c0fe90cd16",
        "name": "Envy",
        "createdAt": "2020-02-04T16:49:23.475Z",
        "updatedAt": "2020-02-04T16:49:23.475Z",
        "commonVotes": 82,
        "deadlyVotes": 75
    },
    {
        "uuid": "4bf84d42-476e-11ea-87f1-f1c0fe90cd16",
        "name": "Glutony",
        "createdAt": "2020-02-04T16:49:23.475Z",
        "updatedAt": "2020-02-04T16:49:23.475Z",
        "commonVotes": 40,
        "deadlyVotes": 21
    },
    {
        "uuid": "4bf84d41-476e-11ea-87f1-f1c0fe90cd16",
        "name": "Lust",
        "createdAt": "2020-02-04T16:49:23.475Z",
        "updatedAt": "2020-02-04T16:49:23.475Z",
        "commonVotes": 17,
        "deadlyVotes": 105
    },
    {
        "uuid": "4bf84d40-476e-11ea-87f1-f1c0fe90cd16",
        "name": "Pride",
        "createdAt": "2020-02-04T16:49:23.475Z",
        "updatedAt": "2020-02-04T16:49:23.475Z",
        "commonVotes": 70,
        "deadlyVotes": 76
    }
]

排序代码:

sins.sort((a, b) => {
  return (a.commonVotes > b.commonVotes) ? 1 : -1
})

我该如何解决这个问题?是光明会放置的一个永远被窃听的虫子吗?

【问题讨论】:

  • 这不是我运行您的代码时所做的。它产生一个排序 17、30、70、82。jsbin.com/fayalexaxu/edit?js,console
  • 顺便说一句,您也没有考虑 a.commonVotes 何时等于 b.commonVotes,尽管这不会影响您的样本数据。
  • 看起来无论您的问题是什么,您在输入问题的过程中无意中修复了它——与您的原始代码进行比较以找到您的答案。
  • 也许你看看这个问题:stackoverflow.com/questions/24080785/…

标签: javascript arrays sorting


【解决方案1】:

试试 asc

sins.sort((x, y) => {
  return (x.commonVotes < y.commonVotes) ? -1 : (x.commonVotes > y.commonVotes) ? 1 : 0
})

试试描述

sins.sort((x, y) => {
  return (x.commonVotes < y.commonVotes) ? 1 : (x.commonVotes > y.commonVotes) ? -1 : 0
})

【讨论】:

    【解决方案2】:

    这似乎工作正常。

    a.sort((a, b) => (a.commonVotes > b.commonVotes) ? 1 : -1);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-02
      • 2011-02-23
      • 1970-01-01
      相关资源
      最近更新 更多