-
Notifications
You must be signed in to change notification settings - Fork 1
/
sortstr_test.go
126 lines (120 loc) · 3.65 KB
/
sortstr_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package sortstr_test
import (
"fmt"
"github.com/stanim/sortstr"
)
// ExampleBy demonstrates multisorting of string slices.
func ExampleBy() {
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
}
// ExampleByHeaders demonstrates multisorting of string
// slices with column headers instead of indices.
func ExampleByHeaders() {
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
}