diff --git a/src/serra/card.go b/src/serra/card.go index 47f26d3..fa8c02c 100644 --- a/src/serra/card.go +++ b/src/serra/card.go @@ -2,6 +2,7 @@ package serra import ( "fmt" + "sort" "strings" "github.com/spf13/cobra" @@ -111,6 +112,14 @@ func Cards(rarity, set, sortby, name, oracle, cardType string) []Card { cards, _ := coll.storage_find(filter, sortStage) + // This is needed because collectornumbers are strings (ie. "23a") but still we + // want it to be sorted numerically ... 1,2,3,10,11,100. + if sortby == "number" { + sort.Slice(cards, func(i, j int) bool { + return filterForDigits(cards[i].CollectorNumber) < filterForDigits(cards[j].CollectorNumber) + }) + } + return cards } diff --git a/src/serra/helpers.go b/src/serra/helpers.go index 835a3c6..3cd560c 100644 --- a/src/serra/helpers.go +++ b/src/serra/helpers.go @@ -3,7 +3,9 @@ package serra import ( "errors" "fmt" + "strconv" "time" + "unicode" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" @@ -167,3 +169,14 @@ func print_price_history(prices []PriceEntry, prefix string) { before = value } } + +func filterForDigits(str string) int { + var numStr string + for _, c := range str { + if unicode.IsDigit(c) { + numStr += string(c) + } + } + s, _ := strconv.Atoi(numStr) + return s +}