From 4d1532b216494f6326e6745fc33de0a06b62032e Mon Sep 17 00:00:00 2001 From: Sung Won Cho Date: Sun, 14 Jan 2018 18:38:35 +1100 Subject: [PATCH] Sort books by note count (#56) --- cmd/ls/ls.go | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/cmd/ls/ls.go b/cmd/ls/ls.go index 844958d7..793a15c0 100644 --- a/cmd/ls/ls.go +++ b/cmd/ls/ls.go @@ -2,6 +2,8 @@ package ls import ( "fmt" + "sort" + "github.com/dnote-io/cli/core" "github.com/dnote-io/cli/infra" "github.com/dnote-io/cli/log" @@ -62,9 +64,32 @@ func newRun(ctx infra.DnoteCtx) core.RunEFunc { } } -func printBooks(dnote infra.Dnote) error { +// bookInfo is an information about the book to be printed on screen +type bookInfo struct { + BookName string + NoteCount int +} + +func getBookInfos(dnote infra.Dnote) []bookInfo { + var ret []bookInfo + for bookName, book := range dnote { - log.Printf("%s \033[%dm(%d)\033[0m\n", bookName, log.ColorYellow, len(book.Notes)) + ret = append(ret, bookInfo{BookName: bookName, NoteCount: len(book.Notes)}) + } + + return ret +} + +func printBooks(dnote infra.Dnote) error { + infos := getBookInfos(dnote) + + // Show books with more notes first + sort.SliceStable(infos, func(i, j int) bool { + return infos[i].NoteCount > infos[j].NoteCount + }) + + for _, info := range infos { + log.Printf("%s \033[%dm(%d)\033[0m\n", info.BookName, log.ColorYellow, info.NoteCount) } return nil