package data import ( "sort" "time" ) type Repository struct { Name string Category string Path string Slug string Description string LastCommit time.Time } type Entry struct { Name string LastCommit time.Time Repositories []*Repository } type Entries struct { Children []*Entry Map map[string]*Entry } func (ent *Entries) Add(r Repository) { if r.Category == "" { ent.Children = append(ent.Children, &Entry{ Name: r.Name, LastCommit: r.LastCommit, Repositories: []*Repository{&r}, }) return } t, ok := ent.Map[r.Category] if !ok { t := &Entry{ Name: r.Category, LastCommit: r.LastCommit, Repositories: []*Repository{&r}, } ent.Map[r.Category] = t return } if t.LastCommit.IsZero() || t.LastCommit.Before(r.LastCommit) { t.LastCommit = r.LastCommit } t.Repositories = append(t.Repositories, &r) } func (ent *Entries) Sort() { sort.Slice(ent.Children, func(i, j int) bool { return ent.Children[i].LastCommit.After(ent.Children[j].LastCommit) }) for _, entries := range ent.Map { sort.Slice(entries.Repositories, func(i, j int) bool { return entries.Repositories[i].LastCommit.After(entries.Repositories[j].LastCommit) }) } }