diff options
author | Thomas Voss <mail@thomasvoss.com> | 2024-08-10 21:32:27 +0200 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2024-08-10 21:32:27 +0200 |
commit | 2c2f850a29b2e896f640679ed9fcec523ccfdb2d (patch) | |
tree | 412662e9494d00135f0ac3bae5816f7aca53767f /templates | |
parent | e1c08fce740b54454c4fea09634bf839ef9f76d3 (diff) |
Support sorting countries by name
Diffstat (limited to 'templates')
-rw-r--r-- | templates/base.go | 35 | ||||
-rw-r--r-- | templates/coins_designs.templ | 2 |
2 files changed, 32 insertions, 5 deletions
diff --git a/templates/base.go b/templates/base.go index ba31baf..76a4da6 100644 --- a/templates/base.go +++ b/templates/base.go @@ -2,12 +2,28 @@ package templates -import "git.thomasvoss.com/euro-cash.eu/i18n" +import ( + "fmt" + "slices" -type country struct{ code, name string } + "git.thomasvoss.com/euro-cash.eu/i18n" + "golang.org/x/text/collate" + "golang.org/x/text/language" +) -func countries(p i18n.Printer) []country { - return []country{ +type country struct { + code, name string +} + +type sortType int + +const ( + sortByCode sortType = iota + sortByName +) + +func countries(p i18n.Printer, sort sortType) []country { + xs := []country{ {code: "AD", name: p.T("Andorra")}, {code: "AT", name: p.T("Austria")}, {code: "BE", name: p.T("Belgium")}, @@ -33,4 +49,15 @@ func countries(p i18n.Printer) []country { {code: "SM", name: p.T("San Marino")}, {code: "VA", name: p.T("Vatican City")}, } + switch sort { + case sortByCode: + case sortByName: + c := collate.New(language.MustParse(p.Locale.Bcp)) + slices.SortFunc(xs, func(x, y country) int { + return c.CompareString(x.name, y.name) + }) + default: + panic(fmt.Sprintf("Attempted to sort by invalid sortType=%d", sort)) + } + return xs } diff --git a/templates/coins_designs.templ b/templates/coins_designs.templ index 1b1ba6d..6d186e1 100644 --- a/templates/coins_designs.templ +++ b/templates/coins_designs.templ @@ -26,7 +26,7 @@ templ CoinsDesigns() { </p> <hr/> <div class="country-grid"> - for _, c := range countries(p) { + for _, c := range countries(p, sortByCode) { <a role="button" class="outline" |