hugo

Unnamed repository; edit this file 'description' to name the repository.

git clone git://git.shimmy1996.com/hugo.git
commit 860c51c314e1f2b06b1424a3b277a2db96fc1f04
parent 855e5869c60ef8f41d5145bbbeeb7a0efbbef468
Author: Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Date:   Sun,  8 May 2022 18:37:14 +0200

tpl/collections: Make sort stable

Fixes #9865

Diffstat:
Mtpl/collections/integration_test.go | 30++++++++++++++++++++++++++++++
Mtpl/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
@@ -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
@@ -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 {