diff options
author | Bjørn Erik Pedersen <[email protected]> | 2022-05-08 18:37:14 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2022-05-08 20:45:54 +0200 |
commit | 860c51c314e1f2b06b1424a3b277a2db96fc1f04 (patch) | |
tree | 3b11a49b0d6a95b185efb0cb5ba1988cdf758d00 /tpl | |
parent | 855e5869c60ef8f41d5145bbbeeb7a0efbbef468 (diff) | |
download | hugo-860c51c314e1f2b06b1424a3b277a2db96fc1f04.tar.gz hugo-860c51c314e1f2b06b1424a3b277a2db96fc1f04.zip |
tpl/collections: Make sort stable
Fixes #9865
Diffstat (limited to 'tpl')
-rw-r--r-- | tpl/collections/integration_test.go | 30 | ||||
-rw-r--r-- | tpl/collections/sort.go | 4 |
2 files changed, 32 insertions, 2 deletions
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 { |