summaryrefslogtreecommitdiffstats
path: root/hugolib/pageSort.go
blob: da068a82546d65e5d9f03df5e369d243b1ecdad1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright © 2014 Steve Francia <spf@spf13.com>.
//
// Licensed under the Simple Public License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://opensource.org/licenses/Simple-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package hugolib

import (
	"sort"
)

/*
 * Implementation of a custom sorter for Pages
 */

// A PageSorter implements the sort interface for Pages
type PageSorter struct {
	pages Pages
	by    PageBy
}

// PageBy is a closure used in the Sort.Less method.
type PageBy func(p1, p2 *Page) bool

func (by PageBy) Sort(pages Pages) {
	ps := &PageSorter{
		pages: pages,
		by:    by, // The Sort method's receiver is the function (closure) that defines the sort order.
	}
	sort.Stable(ps)
}

var DefaultPageSort = func(p1, p2 *Page) bool {
	if p1.Weight == p2.Weight {
		return p1.Date.Unix() > p2.Date.Unix()
	}
	return p1.Weight < p2.Weight
}

func (ps *PageSorter) Len() int      { return len(ps.pages) }
func (ps *PageSorter) Swap(i, j int) { ps.pages[i], ps.pages[j] = ps.pages[j], ps.pages[i] }

// Less is part of sort.Interface. It is implemented by calling the "by" closure in the sorter.
func (ps *PageSorter) Less(i, j int) bool { return ps.by(ps.pages[i], ps.pages[j]) }

func (p Pages) Sort() {
	PageBy(DefaultPageSort).Sort(p)
}

func (p Pages) Limit(n int) Pages {
	if len(p) < n {
		return p[0:n]
	}
	return p
}

func (p Pages) ByWeight() Pages {
	PageBy(DefaultPageSort).Sort(p)
	return p
}

func (p Pages) ByTitle() Pages {
	title := func(p1, p2 *Page) bool {
		return p1.Title < p2.Title
	}

	PageBy(title).Sort(p)
	return p
}

func (p Pages) ByLinkTitle() Pages {
	linkTitle := func(p1, p2 *Page) bool {
		return p1.linkTitle < p2.linkTitle
	}

	PageBy(linkTitle).Sort(p)
	return p
}

func (p Pages) ByDate() Pages {
	date := func(p1, p2 *Page) bool {
		return p1.Date.Unix() < p2.Date.Unix()
	}

	PageBy(date).Sort(p)
	return p
}

func (p Pages) ByPublishDate() Pages {
	pubDate := func(p1, p2 *Page) bool {
		return p1.PublishDate.Unix() < p2.PublishDate.Unix()
	}

	PageBy(pubDate).Sort(p)
	return p
}

func (p Pages) ByLength() Pages {
	length := func(p1, p2 *Page) bool {
		return len(p1.Content) < len(p2.Content)
	}

	PageBy(length).Sort(p)
	return p
}

func (p Pages) Reverse() Pages {
	for i, j := 0, len(p)-1; i < j; i, j = i+1, j-1 {
		p[i], p[j] = p[j], p[i]
	}

	return p
}