mirror of
https://github.com/gohugoio/hugo.git
synced 2025-04-20 22:54:25 +00:00
Added PageSorter and PagesByDate
This commit is contained in:
parent
c7083a5d36
commit
6aa3e51228
1 changed files with 31 additions and 0 deletions
|
@ -45,6 +45,27 @@ func (ip IndexedPages) Pages() Pages {
|
||||||
return pages
|
return pages
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ip IndexedPages) PagesByDate(asc bool) Pages {
|
||||||
|
by := func(p1, p2 *Page) bool {
|
||||||
|
return p1.Date.Unix() < p2.Date.Unix()
|
||||||
|
}
|
||||||
|
|
||||||
|
if asc == false {
|
||||||
|
by = func(p1, p2 *Page) bool {
|
||||||
|
return p1.Date.Unix() > p2.Date.Unix()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ps := &PageSorter{
|
||||||
|
pages: ip.Pages(),
|
||||||
|
by: by,
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Sort(ps)
|
||||||
|
|
||||||
|
return ps.pages
|
||||||
|
}
|
||||||
|
|
||||||
type Index map[string]IndexedPages
|
type Index map[string]IndexedPages
|
||||||
type IndexList map[string]Index
|
type IndexList map[string]Index
|
||||||
|
|
||||||
|
@ -134,3 +155,13 @@ func (s *indexEntrySorter) Swap(i, j int) {
|
||||||
func (s *indexEntrySorter) Less(i, j int) bool {
|
func (s *indexEntrySorter) Less(i, j int) bool {
|
||||||
return s.by(&s.indexEntrys[i], &s.indexEntrys[j])
|
return s.by(&s.indexEntrys[i], &s.indexEntrys[j])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sorting pages
|
||||||
|
type PageSorter struct {
|
||||||
|
pages Pages
|
||||||
|
by func(p1, p2 *Page) bool
|
||||||
|
}
|
||||||
|
|
||||||
|
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] }
|
||||||
|
func (ps *PageSorter) Less(i, j int) bool { return ps.by(ps.pages[i], ps.pages[j]) }
|
||||||
|
|
Loading…
Add table
Reference in a new issue