Simplify sortIssues implementation (#1277)
Some checks failed
CI / test (map[go:1.22.10 golangci:latest]) (push) Has been cancelled
CI / test (map[go:1.23.4 golangci:latest]) (push) Has been cancelled
Security Scan / build (push) Has been cancelled
CI / coverage (push) Has been cancelled

This commit is contained in:
Oleksandr Redko 2024-12-20 11:05:09 +02:00 committed by GitHub
parent 54c2185ae6
commit b12f51f7d6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,7 +1,8 @@
package main package main
import ( import (
"sort" "cmp"
"slices"
"strconv" "strconv"
"strings" "strings"
@ -14,26 +15,14 @@ func extractLineNumber(s string) int {
return lineNumber return lineNumber
} }
type sortBySeverity []*issue.Issue
func (s sortBySeverity) Len() int { return len(s) }
func (s sortBySeverity) Less(i, j int) bool {
if s[i].Severity == s[j].Severity {
if s[i].What == s[j].What {
if s[i].File == s[j].File {
return extractLineNumber(s[i].Line) > extractLineNumber(s[j].Line)
}
return s[i].File > s[j].File
}
return s[i].What > s[j].What
}
return s[i].Severity > s[j].Severity
}
func (s sortBySeverity) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
// sortIssues sorts the issues by severity in descending order // sortIssues sorts the issues by severity in descending order
func sortIssues(issues []*issue.Issue) { func sortIssues(issues []*issue.Issue) {
sort.Sort(sortBySeverity(issues)) slices.SortFunc(issues, func(i, j *issue.Issue) int {
return -cmp.Or(
cmp.Compare(i.Severity, j.Severity),
cmp.Compare(i.What, j.What),
cmp.Compare(i.File, j.File),
cmp.Compare(extractLineNumber(i.Line), extractLineNumber(j.Line)),
)
})
} }