From 910a779f661e50a7aa1b3e974d3e722252488e6c Mon Sep 17 00:00:00 2001 From: pluja Date: Tue, 31 Oct 2023 17:58:19 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=80=20refactor(attributes.go):=20extra?= =?UTF-8?q?ct=20sorting=20logic=20into=20a=20separate=20method=20for=20bet?= =?UTF-8?q?ter=20code=20organization=20and=20reusability?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/database/attributes.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/database/attributes.go b/src/database/attributes.go index e95b908..2a7693e 100644 --- a/src/database/attributes.go +++ b/src/database/attributes.go @@ -34,6 +34,21 @@ func (a *AttributeMap) GetAttributesFromList(attr string) []Attribute { return attributes } +func (a *AttributeMap) GetSortedAttributes(reverse bool) []Attribute { + // Return the attributes sorted by Rating from best to worst + attributes := make([]Attribute, 0, len(a.Attributes)) + for _, attr := range a.Attributes { + attributes = append(attributes, attr) + } + sort.Slice(attributes, func(i, j int) bool { + if reverse { + return attributes[i].Rating > attributes[j].Rating + } + return attributes[i].Rating < attributes[j].Rating + }) + return attributes +} + const ( AttributeRatingInfo = 0 AttributeRatingGood = 1