import (
"sort"
+
+ "github.com/spf13/cast"
)
var spc = newPageCache()
return pages
}
+
+func (p Pages) ByParam(paramsKey interface{}) Pages {
+ paramsKeyStr := cast.ToString(paramsKey)
+ key := "pageSort.ByParam." + paramsKeyStr
+
+ paramsKeyComparator := func(p1, p2 *Page) bool {
+ v1, _ := p1.Param(paramsKeyStr)
+ v2, _ := p2.Param(paramsKeyStr)
+ s1 := cast.ToString(v1)
+ s2 := cast.ToString(v2)
+
+ // Sort nils last.
+ if s1 == "" {
+ return false
+ } else if s2 == "" {
+ return true
+ }
+
+ return s1 < s2
+ }
+
+ pages, _ := spc.get(key, p, pageBy(paramsKeyComparator).Sort)
+
+ return pages
+}
"testing"
"time"
+ "github.com/spf13/cast"
"github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/source"
"github.com/stretchr/testify/assert"
assert.True(t, probablyEqualPages(p2, p1.Reverse()))
}
+func TestPageSortByParam(t *testing.T) {
+ var k interface{} = "arbitrary"
+
+ unsorted := createSortTestPages(10)
+ delete(unsorted[9].Params, cast.ToString(k))
+
+ firstSetValue, _ := unsorted[0].Param(k)
+ secondSetValue, _ := unsorted[1].Param(k)
+ lastSetValue, _ := unsorted[8].Param(k)
+ unsetValue, _ := unsorted[9].Param(k)
+
+ assert.Equal(t, "xyz100", firstSetValue)
+ assert.Equal(t, "xyz99", secondSetValue)
+ assert.Equal(t, "xyz92", lastSetValue)
+ assert.Equal(t, nil, unsetValue)
+
+ sorted := unsorted.ByParam("arbitrary")
+ firstSetSortedValue, _ := sorted[0].Param(k)
+ secondSetSortedValue, _ := sorted[1].Param(k)
+ lastSetSortedValue, _ := sorted[8].Param(k)
+ unsetSortedValue, _ := sorted[9].Param(k)
+
+ assert.Equal(t, firstSetValue, firstSetSortedValue)
+ assert.Equal(t, secondSetValue, lastSetSortedValue)
+ assert.Equal(t, lastSetValue, secondSetSortedValue)
+ assert.Equal(t, unsetValue, unsetSortedValue)
+}
+
func BenchmarkSortByWeightAndReverse(b *testing.B) {
p := createSortTestPages(300)
},
Site: &info,
Source: Source{File: *source.NewFile(filepath.FromSlash(fmt.Sprintf("/x/y/p%d.md", i)))},
+ Params: map[string]interface{}{
+ "arbitrary": "xyz" + fmt.Sprintf("%v", 100-i),
+ },
}
w := 5