Skip to content

Commit

Permalink
Simplify sortIssues implementation (#1277)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear authored Dec 20, 2024
1 parent 54c2185 commit b12f51f
Showing 1 changed file with 10 additions and 21 deletions.
31 changes: 10 additions & 21 deletions cmd/gosec/sort_issues.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package main

import (
"sort"
"cmp"
"slices"
"strconv"
"strings"

Expand All @@ -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)),
)
})
}

0 comments on commit b12f51f

Please sign in to comment.