Repository: gohugoio/hugo (language: go)

Issue:
When sorting a slice of strings (ascending), the literal "infinity" string always comes last
<!-- Please answer these questions before submitting your issue. Thanks! -->

### What version of Hugo are you using (`hugo version`)?

<pre>
$ hugo version
hugo v0.104.3-58b824581360148f2d91f5cc83f69bd22c1aa331+extended linux/amd64 BuildDate=2022-10-04T14:25:23Z VendorInfo=gohugoio
</pre>

### Does this issue reproduce with the latest release?
Yes

### Description of the Issue
Given a slice of strings, Hugo treats "infinity" as alphabetically last in the list.  E.g.:
```
{{delimit (slice "foo" "bar" "infinity" "alpha" "beta" "zeta" "yaml" "xylophone" | sort ) ", "}}
```
Produces:
```
alpha, bar, beta, foo, xylophone, yaml, zeta, infinity
```
It seems like `infinity` is being treated as a numeric value despite being defined as a string among other strings.


Discussion / hints:
Confirmed. 

```text
{{ slice "infinity" "z" "a" | sort }}  --> [a z infinity]
```

This isn't new behavior. Same results with v0.54.0.

Fix patch:
diff --git a/.gitignore b/.gitignore
deleted file mode 100644
index b2aeb914241..00000000000
--- a/.gitignore
+++ /dev/null
@@ -1,29 +0,0 @@
-/hugo
-docs/public*
-/.idea
-.vscode/*
-hugo.exe
-*.test
-*.prof
-nohup.out
-cover.out
-*.swp
-*.swo
-.DS_Store
-*~
-vendor/*/
-*.bench
-*.debug
-coverage*.out
-
-dock.sh
-
-GoBuilds
-dist
-
-hugolib/hugo_stats.json
-resources/sunset.jpg
-
-vendor
-
-.hugo_build.lock
diff --git a/tpl/compare/compare.go b/tpl/compare/compare.go
index 47f77cc99df..2ef5aacfefa 100644
--- a/tpl/compare/compare.go
+++ b/tpl/compare/compare.go
@@ -16,6 +16,7 @@ package compare
 
 import (
 	"fmt"
+	"math"
 	"reflect"
 	"strconv"
 	"time"
@@ -273,7 +274,8 @@ func (ns *Namespace) compareGetWithCollator(collator *langs.Collator, a any, b a
 	case reflect.String:
 		var err error
 		left, err = strconv.ParseFloat(av.String(), 64)
-		if err != nil {
+		// Check if float is a special floating value and cast value as string.
+		if math.IsInf(left, 0) || math.IsNaN(left) || err != nil {
 			str := av.String()
 			leftStr = &str
 		}
@@ -300,7 +302,8 @@ func (ns *Namespace) compareGetWithCollator(collator *langs.Collator, a any, b a
 	case reflect.String:
 		var err error
 		right, err = strconv.ParseFloat(bv.String(), 64)
-		if err != nil {
+		// Check if float is a special floating value and cast value as string.
+		if math.IsInf(right, 0) || math.IsNaN(right) || err != nil {
 			str := bv.String()
 			rightStr = &str
 		}


Test patch:
diff --git a/tpl/compare/compare_test.go b/tpl/compare/compare_test.go
index ce2016b386e..4c50f5f0f87 100644
--- a/tpl/compare/compare_test.go
+++ b/tpl/compare/compare_test.go
@@ -217,6 +217,8 @@ func doTestCompare(t *testing.T, tp tstCompareType, funcUnderTest func(a, b any)
 		{"a", "a", 0},
 		{"a", "b", -1},
 		{"b", "a", 1},
+		{"infinity", "infinity", 0},
+		{"nan", "nan", 0},
 		{tstEqerType1("a"), tstEqerType1("a"), 0},
 		{tstEqerType1("a"), tstEqerType2("a"), 0},
 		{tstEqerType2("a"), tstEqerType1("a"), 0},