Skip to content

Commit

Permalink
CardSorter locale based sorting
Browse files Browse the repository at this point in the history
Updated CardSorter to use locale based sorting (instead of the previous "strip_accents()" method). Fixes a bug in the previous version which resulted in e.g. Czech language cards to be sorted incorrectly. Correct CZ sorting ("sa" > "sc" > "šb > "ta") vs incorrect ("sa" > "šb" > "sc" > "ta"). Works OK even in German (de).
  • Loading branch information
smrky1 committed Jan 28, 2024
1 parent 042bc9e commit e4fcf42
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/domdiv/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import os
import sys
import unicodedata
import locale
from collections import Counter, defaultdict

import configargparse
Expand Down Expand Up @@ -1464,7 +1465,7 @@ def read_card_data(options):


class CardSorter(object):
def __init__(self, order, baseCards):
def __init__(self, order, lang, baseCards):
self.order = order
if order == "global":
self.sort_key = self.by_global_sort_key
Expand Down Expand Up @@ -1498,6 +1499,9 @@ def __init__(self, order, baseCards):
for tag in baseCards:
self.baseCards.append(baseCards[tag])

# Set the locale to the selected language. Necessary for correct sorting using accented characters
locale.setlocale(locale.LC_COLLATE, lang)

# When sorting cards, want to always put "base" cards after all
# kingdom cards, and order the base cards in a particular order
# (ie, all normal treasures by worth, then potion, then all
Expand All @@ -1515,26 +1519,26 @@ def by_global_sort_key(self, card):
return (
int(card.isExpansion()),
self.baseIndex(card.name),
self.strip_accents(card.name),
locale.strxfrm(card.name),
)

def by_expansion_sort_key(self, card):
return (
card.cardset,
int(card.isExpansion()),
self.baseIndex(card.name),
self.strip_accents(card.name),
locale.strxfrm(card.name),
)

def by_colour_sort_key(self, card):
return card.getType().getTypeNames(), self.strip_accents(card.name)
return card.getType().getTypeNames(), locale.strxfrm(card.name)

def by_cost_sort_key(self, card):
return (
card.cardset,
int(card.isExpansion()),
str(card.get_total_cost(card)),
self.strip_accents(card.name),
locale.strxfrm(card.name),
)

@staticmethod
Expand Down Expand Up @@ -1938,6 +1942,7 @@ def filter_sort_cards(cards, options):
# Set up the card sorter
cardSorter = CardSorter(
options.order,
options.language,
{
card.card_tag: card.name
for card in cards
Expand Down

0 comments on commit e4fcf42

Please sign in to comment.