Documentation ¶
Overview ¶
Package sortstr provides sorting methods for string slices based on multiple indices or column headers. Reverse sort is possible by providing negative index values or prefixing column header titles with "-". Note that indices are not zero based, but start with 1. If an index is out of range, an empty string will be used for comparing, rather than throwing a runtime panic.
Normally you only need to use this function:
By(rows, indices)
or these two together:
NewHeaders(titles) ByHeaders(headers, rows, titles)
Only if you need to sort the same rows multiple times in different orders, you'll need to use the Multi type directly.
This was developed with http://godoc.org/github.com/tealeg/xlsx in mind, but can be used independently.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func By ¶
By sorts rows in place by using the indices. If indices are negative, they will be sorted in reverse order.
Example ¶
ExampleBy demonstrates multisorting of string slices.
package main import ( "github.com/stanim/sortstr" ) func main() { var rows = [][]string{ {"John Lennon", "1968", "Let It Be"}, {"John Lennon", "1965", "Let It Be"}, {"John Lennon", "1965", "12-Bar Original"}, {"Paul McCartney", "1963", "All My Loving"}, {"George Harrison", "1968", "While My Guitar Gently Weeps"}, {"Ringo Star", "1965", "Untitled"}, {"Ringo Star"}, } sortstr.By(rows, -1, 3, 2) // reverse order for first column sortstr.Print("By author, title, year", rows, ", ") sortstr.By(rows, 2, 1, 3) sortstr.Print("By year, author, title", rows, ", ") sortstr.By(rows, 3, 1, 2) sortstr.Print("By title, author, year", rows, ", ") }
Output: By author, title, year: Ringo Star Ringo Star, 1965, Untitled Paul McCartney, 1963, All My Loving John Lennon, 1965, 12-Bar Original John Lennon, 1965, Let It Be John Lennon, 1968, Let It Be George Harrison, 1968, While My Guitar Gently Weeps By year, author, title: Ringo Star Paul McCartney, 1963, All My Loving John Lennon, 1965, 12-Bar Original John Lennon, 1965, Let It Be Ringo Star, 1965, Untitled George Harrison, 1968, While My Guitar Gently Weeps John Lennon, 1968, Let It Be By title, author, year: Ringo Star John Lennon, 1965, 12-Bar Original Paul McCartney, 1963, All My Loving John Lennon, 1965, Let It Be John Lennon, 1968, Let It Be Ringo Star, 1965, Untitled George Harrison, 1968, While My Guitar Gently Weeps
func ByHeaders ¶
ByHeaders sorts rows in place by using the column header titles. If titles are prefixed with "-", they will be sorted in reverse order.
Example ¶
ExampleByHeaders demonstrates multisorting of string slices with column headers instead of indices.
package main import ( "fmt" "github.com/stanim/sortstr" ) func main() { titles := []string{"author", "year", "title"} headers := sortstr.NewHeaders(titles) rows := [][]string{ {"John Lennon", "1968", "Let It Be"}, {"John Lennon", "1965", "Let It Be"}, {"John Lennon", "1965", "12-Bar Original"}, {"Paul McCartney", "1963", "All My Loving"}, {"George Harrison", "1968", "While My Guitar Gently Weeps"}, {"Ringo Star", "1965", "Untitled"}, } err := sortstr.ByHeaders(headers, rows, "-author", "title", "year") // reverse order for author if err != nil { fmt.Println("Unknown column header") } sortstr.Print("By -author, title, year", rows, ", ") err = sortstr.ByHeaders(headers, rows, "year", "author", "title") if err != nil { fmt.Println("Unknown column header") } else { sortstr.Print("By year, author, title", rows, ", ") } err = sortstr.ByHeaders(headers, rows, "title", "author", "year") if err != nil { fmt.Println("Unknown column header") } else { sortstr.Print("By title, author, year", rows, ", ") } err = sortstr.ByHeaders(headers, rows, "title", "author", "disc") if err != nil { fmt.Println("Unknown column header") } else { sortstr.Print("By title, author, year", rows, ", ") } }
Output: By -author, title, year: Ringo Star, 1965, Untitled Paul McCartney, 1963, All My Loving John Lennon, 1965, 12-Bar Original John Lennon, 1965, Let It Be John Lennon, 1968, Let It Be George Harrison, 1968, While My Guitar Gently Weeps By year, author, title: Paul McCartney, 1963, All My Loving John Lennon, 1965, 12-Bar Original John Lennon, 1965, Let It Be Ringo Star, 1965, Untitled George Harrison, 1968, While My Guitar Gently Weeps John Lennon, 1968, Let It Be By title, author, year: John Lennon, 1965, 12-Bar Original Paul McCartney, 1963, All My Loving John Lennon, 1965, Let It Be John Lennon, 1968, Let It Be Ringo Star, 1965, Untitled George Harrison, 1968, While My Guitar Gently Weeps Unknown column header
Types ¶
type Headers ¶
Headers defines a map of column indices (int) by header title (string).
func NewHeaders ¶
NewHeaders creates new Headers from column header titles.
type Multi ¶
Multi implements the Sort interface, sorting the rows within, based on multiple column indices. For sorting rows you would normally use the shortcut functions:
sortstr.By sortstr.ByHeaders