diff --git a/tpl/collections/integration_test.go b/tpl/collections/integration_test.go index d767c384c..225eab9fa 100644 --- a/tpl/collections/integration_test.go +++ b/tpl/collections/integration_test.go @@ -43,3 +43,33 @@ baseURL = 'http://example.com/' [foo foo foo] `) } + +// Issue 9865 +func TestSortStable(t *testing.T) { + t.Parallel() + + files := ` +-- config.toml -- +-- layouts/index.html -- +{{ $values := slice (dict "a" 1 "b" 2) (dict "a" 3 "b" 1) (dict "a" 2 "b" 0) (dict "a" 1 "b" 0) (dict "a" 3 "b" 1) (dict "a" 2 "b" 2) (dict "a" 2 "b" 1) (dict "a" 0 "b" 3) (dict "a" 3 "b" 3) (dict "a" 0 "b" 0) (dict "a" 0 "b" 0) (dict "a" 2 "b" 0) (dict "a" 1 "b" 2) (dict "a" 1 "b" 1) (dict "a" 3 "b" 0) (dict "a" 2 "b" 0) (dict "a" 3 "b" 0) (dict "a" 3 "b" 0) (dict "a" 3 "b" 0) (dict "a" 3 "b" 1) }} +Asc: {{ sort (sort $values "b" "asc") "a" "asc" }} +Desc: {{ sort (sort $values "b" "desc") "a" "desc" }} + + ` + + for i := 0; i < 4; i++ { + + b := hugolib.NewIntegrationTestBuilder( + hugolib.IntegrationTestConfig{ + T: t, + TxtarString: files, + }, + ).Build() + + b.AssertFileContent("public/index.html", ` +Asc: [map[a:0 b:0] map[a:0 b:0] map[a:0 b:3] map[a:1 b:0] map[a:1 b:1] map[a:1 b:2] map[a:1 b:2] map[a:2 b:0] map[a:2 b:0] map[a:2 b:0] map[a:2 b:1] map[a:2 b:2] map[a:3 b:0] map[a:3 b:0] map[a:3 b:0] map[a:3 b:0] map[a:3 b:1] map[a:3 b:1] map[a:3 b:1] map[a:3 b:3]] +Desc: [map[a:3 b:3] map[a:3 b:1] map[a:3 b:1] map[a:3 b:1] map[a:3 b:0] map[a:3 b:0] map[a:3 b:0] map[a:3 b:0] map[a:2 b:2] map[a:2 b:1] map[a:2 b:0] map[a:2 b:0] map[a:2 b:0] map[a:1 b:2] map[a:1 b:2] map[a:1 b:1] map[a:1 b:0] map[a:0 b:3] map[a:0 b:0] map[a:0 b:0]] +`) + + } +} diff --git a/tpl/collections/sort.go b/tpl/collections/sort.go index ce7f27771..a0c2f815b 100644 --- a/tpl/collections/sort.go +++ b/tpl/collections/sort.go @@ -177,9 +177,9 @@ func (p pairList) Less(i, j int) bool { // sorts a pairList and returns a slice of sorted values func (p pairList) sort() any { if p.SortAsc { - sort.Sort(p) + sort.Stable(p) } else { - sort.Sort(sort.Reverse(p)) + sort.Stable(sort.Reverse(p)) } sorted := reflect.MakeSlice(p.SliceType, len(p.Pairs), len(p.Pairs)) for i, v := range p.Pairs {