shithub: hugo

Download patch

ref: 04b1a6d997e72d9abada28db22650d38ccbcbb39
parent: dd31e800075eebd78f921df8b4865c238006e7a7
author: Tom <tomaviv57@gmail.com>
date: Sat May 2 08:57:34 EDT 2020

Add support for sort by boolean


--- a/tpl/collections/sort_test.go
+++ b/tpl/collections/sort_test.go
@@ -218,6 +218,9 @@
 				map[interface{}]interface{}{"Title": "Foo", "Weight": 10},
 			},
 		},
+		// test boolean values
+		{[]bool{false, true, false}, "value", "asc", []bool{false, false, true}},
+		{[]bool{false, true, false}, "value", "desc", []bool{true, false, false}},
 		// test error cases
 		{(*[]TstX)(nil), nil, "asc", false},
 		{TstX{A: "a", B: "b"}, nil, "asc", false},
--- a/tpl/compare/compare.go
+++ b/tpl/compare/compare.go
@@ -252,6 +252,11 @@
 		case timeType:
 			left = float64(toTimeUnix(av))
 		}
+	case reflect.Bool:
+		left = 0
+		if av.Bool() {
+			left = 1
+		}
 	}
 
 	bv := reflect.ValueOf(b)
@@ -274,6 +279,11 @@
 		switch bv.Type() {
 		case timeType:
 			right = float64(toTimeUnix(bv))
+		}
+	case reflect.Bool:
+		right = 0
+		if bv.Bool() {
+			right = 1
 		}
 	}