-
Notifications
You must be signed in to change notification settings - Fork 1
/
sort_example_test.go
116 lines (104 loc) · 1.96 KB
/
sort_example_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
// This file shows how to implement sort.Interface for LGE slices.
package intern_test
import (
"fmt"
"sort"
"github.com/spakin/intern"
)
// LGESlice is a slice of LGEs that implements sort.Interface.
type LGESlice []intern.LGE
// Len returns the length of an LGESlice.
func (ls LGESlice) Len() int { return len(ls) }
// Less reports whether one element of an LGESlice is less than another.
func (ls LGESlice) Less(i, j int) bool { return ls[i] < ls[j] }
// Swap swaps two elements of an LGESLice.
func (ls LGESlice) Swap(i, j int) { ls[i], ls[j] = ls[j], ls[i] }
// Sort a list of strings by interning them to LGE symbols.
func ExamplePreLGE() {
// Define some strings.
sList := []string{
"Gerontius",
"Reginard",
"Hildigrim",
"Eglantine",
"Diamond",
"Adamanta",
"Sigismond",
"Adalgrim",
"Flambard",
"Paladin",
"Peregrin",
"Pimpernel",
"Everard",
"Ferdibrand",
"Pervinca",
"Lalia",
"Ferdinand",
"Isembard",
"Isembold",
"Hildifons",
"Isengrim",
"Faramir",
"Isengar",
"Pearl",
"Goldilocks",
"Fortinbras",
"Isumbras",
"Bandobras",
"Adelard",
"Hildigard",
"Hildibrand",
"Rosa",
}
// Indicate our intent to intern all of the strings.
for _, s := range sList {
intern.PreLGE(s)
}
// Intern each string into an LGE and store it in an LGESlice.
syms := make(LGESlice, len(sList))
for i, s := range sList {
l, err := intern.NewLGE(s)
if err != nil {
panic(err)
}
syms[i] = l
}
// Sort the LGESlice and output the result.
sort.Sort(syms)
for _, s := range syms {
fmt.Println(s)
}
// Output:
// Adalgrim
// Adamanta
// Adelard
// Bandobras
// Diamond
// Eglantine
// Everard
// Faramir
// Ferdibrand
// Ferdinand
// Flambard
// Fortinbras
// Gerontius
// Goldilocks
// Hildibrand
// Hildifons
// Hildigard
// Hildigrim
// Isembard
// Isembold
// Isengar
// Isengrim
// Isumbras
// Lalia
// Paladin
// Pearl
// Peregrin
// Pervinca
// Pimpernel
// Reginard
// Rosa
// Sigismond
}