From 5f16ca42d5779eba2e5a2cb8fae14c490eee547b Mon Sep 17 00:00:00 2001 From: adnano Date: Sat, 2 Oct 2021 18:08:01 -0400 Subject: [PATCH] page: Sort Dirs --- page.go | 51 ++++++++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/page.go b/page.go index 5c88185..18d2343 100644 --- a/page.go +++ b/page.go @@ -286,29 +286,8 @@ func (p *Page) writeTo(dstPath string, task *Task) error { // sort sorts the directory's pages by weight, then date, then filepath. func (p *Page) sort() { - sort.Slice(p.Pages, func(i, j int) bool { - pi, pj := p.Pages[i], p.Pages[j] - return pi.FilePath < pj.FilePath - }) - - sort.SliceStable(p.Pages, func(i, j int) bool { - pi, pj := p.Pages[i], p.Pages[j] - return pi.Date.After(pj.Date) - }) - - sort.SliceStable(p.Pages, func(i, j int) bool { - pi, pj := p.Pages[i], p.Pages[j] - return pi.Weight < pj.Weight - }) - - for i := range p.Pages { - if i-1 >= 0 { - p.Pages[i].Prev = p.Pages[i-1] - } - if i+1 < len(p.Pages) { - p.Pages[i].Next = p.Pages[i+1] - } - } + sortPages(p.Pages) + sortPages(p.Dirs) // Sort subdirectories for _, d := range p.Dirs { @@ -316,6 +295,32 @@ func (p *Page) sort() { } } +func sortPages(pages []*Page) { + sort.Slice(pages, func(i, j int) bool { + pi, pj := pages[i], pages[j] + return pi.FilePath < pj.FilePath + }) + + sort.SliceStable(pages, func(i, j int) bool { + pi, pj := pages[i], pages[j] + return pi.Date.After(pj.Date) + }) + + sort.SliceStable(pages, func(i, j int) bool { + pi, pj := pages[i], pages[j] + return pi.Weight < pj.Weight + }) + + for i := range pages { + if i-1 >= 0 { + pages[i].Prev = pages[i-1] + } + if i+1 < len(pages) { + pages[i].Next = pages[i+1] + } + } +} + // execute runs a command. func execute(command string, input io.Reader, output io.Writer) error { split := strings.Split(command, " ")