backend: sort blogposts in the index

This commit is contained in:
Cadey Ratio 2016-12-14 08:56:15 -08:00
parent 07fa595e3e
commit 305db738ea
1 changed files with 16 additions and 1 deletions

View File

@ -8,7 +8,9 @@ import (
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"sort"
"strings" "strings"
"time"
"github.com/gernest/front" "github.com/gernest/front"
) )
@ -20,7 +22,18 @@ type Post struct {
Date string `json:"date"` Date string `json:"date"`
} }
var posts []*Post type Posts []*Post
func (p Posts) Len() int { return len(p) }
func (p Posts) Less(i, j int) bool {
iDate, _ := time.Parse("2006-01-02", p[i].Date)
jDate, _ := time.Parse("2006-01-02", p[j].Date)
return iDate.Unix() < jDate.Unix()
}
func (p Posts) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
var posts Posts
func init() { func init() {
err := filepath.Walk("./blog/", func(path string, info os.FileInfo, err error) error { err := filepath.Walk("./blog/", func(path string, info os.FileInfo, err error) error {
@ -59,6 +72,8 @@ func init() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
sort.Sort(posts)
} }
func main() { func main() {