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 ed1bb5c5a4
commit 927c568059
2 changed files with 22 additions and 1 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

@ -4,7 +4,9 @@ import (
"errors" "errors"
"fmt" "fmt"
"math" "math"
"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"
@ -166,7 +168,6 @@ func print_price_history(prices []PriceEntry, prefix string) {
var before float64 var before float64
for _, e := range prices { for _, e := range prices {
// TODO: Make currency configurable
value := e.Usd value := e.Usd
if getCurrency() == "EUR" { if getCurrency() == "EUR" {
value = e.Eur value = e.Eur
@ -205,3 +206,14 @@ func getFloat64(unknown interface{}) (float64, error) {
return math.NaN(), errors.New("Non-numeric type could not be converted to float") return math.NaN(), errors.New("Non-numeric type could not be converted to float")
} }
} }
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
}