0

I am trying to sort an array in a decending fashion by the property commonVotes. Sorting is going somewhat well except the last element (Pride) after being sorted isn't going in the correct place.

Array before sorting:

[
    {
        "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
    }
]

Array after sorting:

[
    {
        "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
    }
]

Sorting code:

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

How do I fix this? Is it a bug placed by the Illuminati that is forver bugged?

Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
Jake Cross
  • 523
  • 1
  • 6
  • 14
  • That's not what your code does when I run it. It yields an ordering 17, 30, 70, 82. https://jsbin.com/fayalexaxu/edit?js,console – Mike 'Pomax' Kamermans Feb 04 '20 at 17:33
  • btw, you are also not taking into account when a.commonVotes equals b.commonVotes, although that would not affect your sample data. – willman Feb 04 '20 at 17:37
  • 2
    Looks like whatever your problem is, you inadvertently fixed it in the process of typing up your question -- compare to your original code to find your answer. – willman Feb 04 '20 at 17:40
  • maybe you have a look to this question: https://stackoverflow.com/questions/24080785/sorting-in-javascript-shouldnt-returning-a-boolean-be-enough-for-a-comparison – Nina Scholz Feb 04 '20 at 17:59

2 Answers2

0

Try asc

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

Try desc

sins.sort((x, y) => {
  return (x.commonVotes < y.commonVotes) ? 1 : (x.commonVotes > y.commonVotes) ? -1 : 0
})
Dave Keane
  • 729
  • 8
  • 19
0

This appears to be working fine.

a.sort((a, b) => (a.commonVotes > b.commonVotes) ? 1 : -1);
Evan
  • 2,120
  • 1
  • 15
  • 20