Ishadijcks / Ishadijcks.github.io

PokéClicker
15 stars 32 forks source link

Sort by ID first to maintain same order #22

Closed moriakaice closed 8 years ago

moriakaice commented 8 years ago

Hopefully, sorting by ID first should maintain order for the same values

Ishadijcks commented 8 years ago

Unfortunately, that is not how sorting works. The first sort is overwritten by the second one.

I do appreciate that you're trying to help, so I'll give you a hint on how to keep the right order when sorting on the same values.

When 2 Pokémon have the same attack value, the compare function returns 0. This means that they are equal. 2 Pokémon that are equal are 'randomly' placed above and below each other. This is not what we want, because then they can switch at random.

We need a way to indicate that 2 pokemon with the same attack should always be placed in the same order...

moriakaice commented 8 years ago

I tend to disagree, this is the most common approach in JS to apply secondary sorting.

How this works is: 1.) order the list by ID; 2.) order the list by attack -> in case it's the same, do nothing (so leave it with sort by ID).

Give it a go, compare to the website, you'll see that.

Ishadijcks commented 8 years ago

Weird, because the first google result for JS secondary sort all do it the other way.

http://stackoverflow.com/questions/11824833/add-secondary-sort-to-array-output http://stackoverflow.com/questions/2784230/javascript-how-do-you-sort-an-array-on-multiple-columns http://jasonmenayan.com/secondary-sorting-of-arrays-in-javascript/

This is also 'twice' as fast, because you only need to go through the list once.