From f9277631d4870c8085ecd994006d2b3212e6c9bb Mon Sep 17 00:00:00 2001 From: hazedav Date: Fri, 24 Feb 2023 08:40:35 -0600 Subject: [PATCH] fix(lwseverity): use stable sorting for severity (#1161) GROW-1432 --- lwseverity/severity.go | 4 ++-- lwseverity/severity_test.go | 34 ++++++++++++++++------------------ 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/lwseverity/severity.go b/lwseverity/severity.go index 7626581a7..7235f7247 100644 --- a/lwseverity/severity.go +++ b/lwseverity/severity.go @@ -153,7 +153,7 @@ func ShouldFilter(severity, threshold string) bool { // Sort a slice of Severity interfaces from critical -> info func SortSlice[S Severity](s []S) { - sort.Slice(s, func(i, j int) bool { + sort.SliceStable(s, func(i, j int) bool { sevI, _ := Normalize(s[i].GetSeverity()) sevJ, _ := Normalize(s[j].GetSeverity()) return sevI < sevJ @@ -162,7 +162,7 @@ func SortSlice[S Severity](s []S) { // Sort a slice of Severity interfaces from info -> critical func SortSliceA[S Severity](s []S) { - sort.Slice(s, func(i, j int) bool { + sort.SliceStable(s, func(i, j int) bool { sevI, _ := Normalize(s[i].GetSeverity()) sevJ, _ := Normalize(s[j].GetSeverity()) return sevI > sevJ diff --git a/lwseverity/severity_test.go b/lwseverity/severity_test.go index cea19514a..e9a0ffae6 100644 --- a/lwseverity/severity_test.go +++ b/lwseverity/severity_test.go @@ -89,6 +89,7 @@ func TestShouldFilterTest(t *testing.T) { } type myStruct struct { + alertID int severity string } @@ -100,31 +101,28 @@ type myStructs []myStruct func TestSort(t *testing.T) { m := myStructs{ - myStruct{ - severity: "Low", - }, - myStruct{ - severity: "High", - }, + myStruct{alertID: 6, severity: "Low"}, + myStruct{alertID: 9, severity: "High"}, + myStruct{alertID: 1, severity: "Low"}, + myStruct{alertID: 3, severity: "Low"}, + myStruct{alertID: 8, severity: "Low"}, } expected := myStructs{ - myStruct{ - severity: "High", - }, - myStruct{ - severity: "Low", - }, + myStruct{alertID: 9, severity: "High"}, + myStruct{alertID: 6, severity: "Low"}, + myStruct{alertID: 1, severity: "Low"}, + myStruct{alertID: 3, severity: "Low"}, + myStruct{alertID: 8, severity: "Low"}, } lwseverity.SortSlice(m) assert.Equal(t, expected, m) expected = myStructs{ - myStruct{ - severity: "Low", - }, - myStruct{ - severity: "High", - }, + myStruct{alertID: 6, severity: "Low"}, + myStruct{alertID: 1, severity: "Low"}, + myStruct{alertID: 3, severity: "Low"}, + myStruct{alertID: 8, severity: "Low"}, + myStruct{alertID: 9, severity: "High"}, } lwseverity.SortSliceA(m) assert.Equal(t, expected, m)