Update sort to handle the case where the source data is out of order

This commit is contained in:
Tom
2021-06-07 19:36:04 -04:00
parent 6ab791d762
commit 0278162dce
3 changed files with 8 additions and 3 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -108,11 +108,16 @@ export default function UpdateBuildings() {
arr = Object.keys(CacheObjectsNextAchievement).map(k => { arr = Object.keys(CacheObjectsNextAchievement).map(k => {
const o = {}; const o = {};
o.name = k; o.name = k;
o.id = Game.Objects[k].id;
o.amountUntilNext = CacheObjectsNextAchievement[k].AmountNeeded; o.amountUntilNext = CacheObjectsNextAchievement[k].AmountNeeded;
o.priceUntilNext = CacheObjectsNextAchievement[k].price; o.priceUntilNext = CacheObjectsNextAchievement[k].price;
return o; return o;
}); });
// Sort by price until next achievement. Buildings that aren't close to an achievement are always last. // First, sort using default order.
arr.sort((a, b) => a.id - b.id);
// Sort by price until next achievement.
// Buildings that aren't within 100 of an achievement are placed at the end, still in
// default order relative to each other because sort() is guaranteed stable.
arr.sort((a, b) => arr.sort((a, b) =>
(a.amountUntilNext !== 101 ? a.priceUntilNext : Infinity) - (a.amountUntilNext !== 101 ? a.priceUntilNext : Infinity) -
(b.amountUntilNext !== 101 ? b.priceUntilNext : Infinity) (b.amountUntilNext !== 101 ? b.priceUntilNext : Infinity)