From 6aa3e512280e7b11d44ac6effb4e01e4e2c7af66 Mon Sep 17 00:00:00 2001
From: Henry <cryptix@riseup.net>
Date: Sun, 24 Nov 2013 22:10:20 +0100
Subject: [PATCH] Added PageSorter and PagesByDate

---
 hugolib/index.go | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/hugolib/index.go b/hugolib/index.go
index 2c690e18..f39723e4 100644
--- a/hugolib/index.go
+++ b/hugolib/index.go
@@ -45,6 +45,27 @@ func (ip IndexedPages) Pages() 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 IndexList map[string]Index
 
@@ -134,3 +155,13 @@ func (s *indexEntrySorter) Swap(i, j int) {
 func (s *indexEntrySorter) Less(i, j int) bool {
 	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]) }
-- 
2.30.2