From b12f51f7d688ab7f51c543813efbb984d466adab Mon Sep 17 00:00:00 2001 From: Oleksandr Redko Date: Fri, 20 Dec 2024 11:05:09 +0200 Subject: [PATCH] Simplify sortIssues implementation (#1277) --- cmd/gosec/sort_issues.go | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/cmd/gosec/sort_issues.go b/cmd/gosec/sort_issues.go index 04c1d3d..c155cbf 100644 --- a/cmd/gosec/sort_issues.go +++ b/cmd/gosec/sort_issues.go @@ -1,7 +1,8 @@ package main import ( - "sort" + "cmp" + "slices" "strconv" "strings" @@ -14,26 +15,14 @@ func extractLineNumber(s string) int { 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 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)), + ) + }) }