From 2c2f850a29b2e896f640679ed9fcec523ccfdb2d Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Sat, 10 Aug 2024 21:32:27 +0200 Subject: Support sorting countries by name --- templates/base.go | 35 +++++++++++++++++++++++++++++++---- templates/coins_designs.templ | 2 +- 2 files changed, 32 insertions(+), 5 deletions(-) (limited to 'templates') 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() {


- for _, c := range countries(p) { + for _, c := range countries(p, sortByCode) {