Sort on card numbers is now numeric. fixes #8

This commit is contained in:
Florian Baumann 2023-04-17 14:16:53 +02:00
parent a11c17fbf5
commit b5139e8af4
2 changed files with 22 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package serra
import ( import (
"fmt" "fmt"
"sort"
"strings" "strings"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -111,6 +112,14 @@ func Cards(rarity, set, sortby, name, oracle, cardType string) []Card {
cards, _ := coll.storage_find(filter, sortStage) 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 return cards
} }

View File

@ -3,7 +3,9 @@ package serra
import ( import (
"errors" "errors"
"fmt" "fmt"
"strconv"
"time" "time"
"unicode"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
@ -167,3 +169,14 @@ func print_price_history(prices []PriceEntry, prefix string) {
before = value 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
}