Skip to content

Commit

Permalink
fix: sort algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
chrysomallos committed Aug 17, 2024
1 parent 903fed7 commit bbb830f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/encoder.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ export default class Encoder {
for (let count = 3; count > 0; count--) {
//build the map of set and faction combinations within the group of similar card counts
const factionSetsMap = grouped[count].reduce((map, card) => {
const sf = card.set * 100 + card.factionId;
if (map.has(sf)) map.get(sf).push(card);
else map.set(sf, [card]);
const setFaction = card.set * 100 + card.factionId;
if (map.has(setFaction)) map.get(setFaction).push(card);
else map.set(setFaction, [card]);
return map;
}, new Map());

Expand All @@ -101,7 +101,9 @@ export default class Encoder {
//First by the number of set/faction combinations in each top-level list
//Second by the alphanumeric order of the card codes within those lists.
[...factionSetsMap.entries()]
.sort(([a], [b]) => a - b)
.sort(([firstSetFaction, {length: firstCardsLength}], [secondSetFaction, {length: secondCardLength}]) =>
firstCardsLength - secondCardLength !== 0 ? firstCardsLength - secondCardLength : firstSetFaction - secondSetFaction
)
.forEach(([, groupCards]) => {
const [firstCard] = groupCards;
values.push(groupCards.length);
Expand Down
2 changes: 1 addition & 1 deletion test/deck.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('[Deck] class tests', function () {
'01SI003:3', '01SI018:3', '01SI047:3', '01NX004:3', '01NX019:3', '01NX021:3', '01NX052:3', '04NX002:3', '04NX017:3', '01SI022:2',
'01SI028:2', '01SI029:2', '01SI046:2', '01NX056:2', '04SI017:1', '04NX001:1', '04NX015:1'
]);
assert.equal(deck.code, 'CEBQIAIDAQJRKNADAECQGERPAICAGAQRAIAQCAZYAQAQKFQ4DUXAEAQEAMAQ6AIEAUIQ');
assert.equal(deck.code, 'CEBQEBADAIIQGAIFAMJC6BABAMCBGFJUAIAQCAZYAQAQKFQ4DUXAEAIEAUIQEBADAEHQ');
const deckOrder = Deck.fromCode(deck.code);
assert.deepEqual(deck.list.sort(), deckOrder.list.sort());
});
Expand Down

0 comments on commit bbb830f

Please sign in to comment.