sortstr

package module
v0.0.0-...-b1c3368 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 21, 2016 License: MIT Imports: 3 Imported by: 0

README

sortstr

Travis CI codecov goreportcard Documentation and Examples Software License

Sort slices of strings 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 the xlsx package in mind, but can be used independently.

Examples

Using indices:

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

Using column headers:

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
Documentation

See godoc for more documentation and examples.

License

Released under the MIT License.

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

func By(rows [][]string, indices ...int)

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

func ByHeaders(headers Headers, rows [][]string,
	titles ...string) error

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

func Print

func Print(label string, rows [][]string, sep string)

Print rows (mostly for debugging purposes). See the examples of By and ByHeaders.

Types

type Headers

type Headers map[string]int

Headers defines a map of column indices (int) by header title (string).

func NewHeaders

func NewHeaders(titles []string) Headers

NewHeaders creates new Headers from column header titles.

func (Headers) Index

func (hs Headers) Index(title string) (int, error)

Index of a given column header title

func (Headers) Indices

func (hs Headers) Indices(titles ...string) (
	[]int, error)

Indices of given column header titles

type Multi

type Multi struct {
	Rows    [][]string
	Indices []int
}

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

func (*Multi) Len

func (m *Multi) Len() int

Len is part of sort.Interface.

func (*Multi) Less

func (m *Multi) Less(i, j int) bool

Less is part of sort.Interface. It is implemented by looping along the indices until it finds a comparison that is either Less or !Less. Note that it can call the indices comparisons twice per call.

func (*Multi) Sort

func (m *Multi) Sort()

Sort sorts the argument slice according to its indices

func (*Multi) Swap

func (m *Multi) Swap(i, j int)

Swap is part of sort.Interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL