Skip to content

Commit

Permalink
Sort on card numbers is now numeric. fixes #8
Browse files Browse the repository at this point in the history
  • Loading branch information
noqqe committed Apr 17, 2023
1 parent a11c17f commit b5139e8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/serra/card.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package serra

import (
"fmt"
"sort"
"strings"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -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
}

Expand Down
13 changes: 13 additions & 0 deletions src/serra/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

0 comments on commit b5139e8

Please sign in to comment.