summaryrefslogtreecommitdiffstats
path: root/hugolib
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2015-07-25 16:34:35 +0200
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2015-07-25 16:34:35 +0200
commit36e0d005ed5bc680c5816abd1917dd392b7b92de (patch)
tree23e5c7e2e1d10d21320ed63e5c89debdefc88e2a /hugolib
parent6674189bc2eb85f0d8c8d0aa9c4bb64fe731d9a0 (diff)
Fall back to link title for default page sort
Fixes #1299
Diffstat (limited to 'hugolib')
-rw-r--r--hugolib/pageSort.go4
-rw-r--r--hugolib/pageSort_test.go35
2 files changed, 39 insertions, 0 deletions
diff --git a/hugolib/pageSort.go b/hugolib/pageSort.go
index d42bb40dc..b4bc9d2a3 100644
--- a/hugolib/pageSort.go
+++ b/hugolib/pageSort.go
@@ -15,6 +15,7 @@ package hugolib
import (
"sort"
+ "strings"
)
var spc = newPageCache()
@@ -42,6 +43,9 @@ func (by PageBy) Sort(pages Pages) {
var DefaultPageSort = func(p1, p2 *Page) bool {
if p1.Weight == p2.Weight {
+ if p1.Date.Unix() == p2.Date.Unix() {
+ return strings.Compare(p1.LinkTitle(), p2.LinkTitle()) == 1
+ }
return p1.Date.Unix() > p2.Date.Unix()
}
return p1.Weight < p2.Weight
diff --git a/hugolib/pageSort_test.go b/hugolib/pageSort_test.go
index cd999f7a3..4a16ab87d 100644
--- a/hugolib/pageSort_test.go
+++ b/hugolib/pageSort_test.go
@@ -5,10 +5,37 @@ import (
"github.com/stretchr/testify/assert"
"path/filepath"
"testing"
+ "time"
"github.com/spf13/hugo/source"
)
+func TesDefaultSort(t *testing.T) {
+
+ d1 := time.Now()
+ d2 := d1.Add(1 * time.Hour)
+ d3 := d1.Add(2 * time.Hour)
+
+ p := createSortTestPages(3)
+
+ // first by weight
+ setSortVals([3]time.Time{d1, d2, d3}, [3]string{"a", "b", "c"}, [3]int{3, 2, 1}, p)
+ p.Sort()
+ assert.Equal(t, 1, p[0].Weight)
+
+ // next by date
+ setSortVals([3]time.Time{d3, d1, d2}, [3]string{"a", "b", "c"}, [3]int{1, 1, 1}, p)
+ p.Sort()
+ assert.Equal(t, d1, p[0].Date)
+
+ // finally by title
+ setSortVals([3]time.Time{d3, d3, d3}, [3]string{"b", "a", "c"}, [3]int{1, 1, 1}, p)
+ p.Sort()
+ assert.Equal(t, "a", p[0].Title)
+ assert.Equal(t, "b", p[1].Title)
+ assert.Equal(t, "c", p[2].Title)
+}
+
func TestPageSortReverse(t *testing.T) {
p1 := createSortTestPages(10)
assert.Equal(t, 0, p1[0].FuzzyWordCount)
@@ -28,6 +55,14 @@ func BenchmarkSortByWeightAndReverse(b *testing.B) {
for i := 0; i < b.N; i++ {
p = p.ByWeight().Reverse()
}
+}
+
+func setSortVals(dates [3]time.Time, titles [3]string, weights [3]int, pages Pages) {
+ for i := range dates {
+ pages[i].Date = dates[i]
+ pages[i].Weight = weights[i]
+ pages[i].Title = titles[i]
+ }
}