diff --git a/compiler/book.go b/compiler/book.go index cbaa31b..871199c 100644 --- a/compiler/book.go +++ b/compiler/book.go @@ -68,6 +68,8 @@ var bookTempl = ` func MakeBook(path string, opts *settings.Settings) { // Everything needs to be compiled CompileDir(path, opts) + // Sort scores + sort.Sort(settings.ScoresByName{scores}) // Compile the book template var templ = template.Must(template.New("songBook").Funcs(template.FuncMap{ "in": helpers.InSlice, diff --git a/settings/score.go b/settings/score.go index 1281a5e..b71b6b7 100644 --- a/settings/score.go +++ b/settings/score.go @@ -127,3 +127,25 @@ func (s *Score) GenerateOutputPath(opts *Settings) { file = file[:dot+1] + "pdf" s.OutputPath = filepath.Join(opts.OutputDir, file) } + +// Scores aliases a slice of scores +type Scores []Score + +// ScoresByName implements sort.Interface by providing Less and using the Len and +// Swap methods of the embedded Scores value. +type ScoresByName struct{ Scores } + +// Len returns the number of scores and +// implements part of the sort.Interface +func (s Scores) Len() int { return len(s) } + +// Swap interchanges the position of two scores and +// implements part of the sort.Interface +func (s Scores) Swap(i, j int) { s[i], s[j] = s[j], s[i] } + +// Less reports whether the name of the score on position i is +// smaller than the name of the score on position j +// and implements part of the sort.Interface +func (s ScoresByName) Less(i, j int) bool { + return s.Scores[i].Name < s.Scores[j].Name +}