aboutsummaryrefslogtreecommitdiff
path: root/page/pagelist.go
blob: 298acf94c703a36dc30fe158871382f5691b68c0 (plain) (blame)
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
package page

import (
	"sort"
)

// PageList is a slice of pages, providing a couple of methods to sort
// by the date, or date reversed.
type PageList []*Page

// RemoveDateless returns two PageLists, the first with valid dates,
// and the second without. This is useful if you need a PageList which
// will run SortDate and SortDateReverse without issue.
func (p PageList) RemoveDateless() (PageList, PageList) {
	with := PageList{}
	without := PageList{}

	for _, p := range p {
		if p.Date != nil {
			with = append(with, p)
		} else {
			without = append(without, p)
		}
	}

	return with, without
}

// SortDate returns the pagelist sorted by date, may panic if pages do
// not all have dates.
func (p PageList) SortDate() PageList {
	sort.Slice(p, func(i, j int) bool {
		return p[i].Time().After(p[j].Time())
	})

	return p
}

// SortDateReverse returns the pagelist sorted by date in reverse, may panic if
// pages do not all have dates.
func (p PageList) SortDateReverse() PageList {
	sort.Slice(p, func(i, j int) bool {
		return p[i].Time().Before(p[j].Time())
	})

	return p
}