Skip to content

Commit 386f927

Browse files
committed
fix(sort): sort integers as such rather than strings
1 parent 7891132 commit 386f927

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

internal/template/sort.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ func getFieldAsString(item interface{}, path string) string {
9090
func (s sortableByKey) Less(i, j int) bool {
9191
dataI := getFieldAsString(s.data[i], s.key)
9292
dataJ := getFieldAsString(s.data[j], s.key)
93+
94+
if intI, err := strconv.ParseInt(dataI, 10, 64); err == nil {
95+
if intJ, err := strconv.ParseInt(dataJ, 10, 64); err == nil {
96+
// If both are integers, compare as integers
97+
return intI < intJ
98+
}
99+
}
100+
93101
return dataI < dataJ
94102
}
95103

internal/template/sort_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func TestSortObjectsByKeys(t *testing.T) {
5555
Env: map[string]string{
5656
"VIRTUAL_HOST": "bar.localhost",
5757
},
58-
ID: "9",
58+
ID: "11",
5959
}
6060
o1 := &context.RuntimeContainer{
6161
Created: time.Date(2021, 1, 2, 0, 0, 10, 0, time.UTC),

0 commit comments

Comments
 (0)