forked from tinygo-org/tinygo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort.go
183 lines (167 loc) · 2.91 KB
/
sort.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import "sort"
// sort.Slice implicitly uses reflect.Swapper
func strings() {
data := []string{"aaaa", "cccc", "bbb", "fff", "ggg"}
sort.Slice(data, func(i, j int) bool {
return data[i] > data[j]
})
println("strings")
for _, d := range data {
println(d)
}
}
func int64s() {
sd := []int64{1, 6, 3, 2, 1923, 123, -123, -29, 3, 0, 1}
sort.Slice(sd, func(i, j int) bool {
return sd[i] > sd[j]
})
println("int64s")
for _, d := range sd {
println(d)
}
ud := []uint64{1, 6, 3, 2, 1923, 123, 29, 3, 0, 1}
sort.Slice(ud, func(i, j int) bool {
return ud[i] > ud[j]
})
println("uint64s")
for _, d := range ud {
println(d)
}
}
func int32s() {
sd := []int32{1, 6, 3, 2, 1923, 123, -123, -29, 3, 0, 1}
sort.Slice(sd, func(i, j int) bool {
return sd[i] > sd[j]
})
println("int32s")
for _, d := range sd {
println(d)
}
ud := []uint32{1, 6, 3, 2, 1923, 123, 29, 3, 0, 1}
sort.Slice(ud, func(i, j int) bool {
return ud[i] > ud[j]
})
println("uint32s")
for _, d := range ud {
println(d)
}
}
func int16s() {
sd := []int16{1, 6, 3, 2, 1923, 123, -123, -29, 3, 0, 1}
sort.Slice(sd, func(i, j int) bool {
return sd[i] > sd[j]
})
println("int16s")
for _, d := range sd {
println(d)
}
ud := []uint16{1, 6, 3, 2, 1923, 123, 29, 3, 0, 1}
sort.Slice(ud, func(i, j int) bool {
return ud[i] > ud[j]
})
println("uint16s")
for _, d := range ud {
println(d)
}
}
func int8s() {
sd := []int8{1, 6, 3, 2, 123, -123, -29, 3, 0, 1}
sort.Slice(sd, func(i, j int) bool {
return sd[i] > sd[j]
})
println("int8s")
for _, d := range sd {
println(d)
}
ud := []uint8{1, 6, 3, 2, 123, 29, 3, 0, 1}
sort.Slice(ud, func(i, j int) bool {
return ud[i] > ud[j]
})
println("uint8s")
for _, d := range ud {
println(d)
}
}
func ints() {
sd := []int{1, 6, 3, 2, 123, -123, -29, 3, 0, 1}
sort.Slice(sd, func(i, j int) bool {
return sd[i] > sd[j]
})
println("ints")
for _, d := range sd {
println(d)
}
ud := []uint{1, 6, 3, 2, 123, 29, 3, 0, 1}
sort.Slice(ud, func(i, j int) bool {
return ud[i] > ud[j]
})
println("uints")
for _, d := range ud {
println(d)
}
}
func structs() {
type s struct {
name string
a uint64
b uint32
c uint16
d int
e *struct {
aa uint16
bb int
}
}
data := []s{
{
name: "struct 1",
d: 100,
e: &struct {
aa uint16
bb int
}{aa: 123, bb: -10},
},
{
name: "struct 2",
d: 1,
e: &struct {
aa uint16
bb int
}{aa: 15, bb: 10},
},
{
name: "struct 3",
d: 10,
e: &struct {
aa uint16
bb int
}{aa: 31, bb: -1030},
},
{
name: "struct 4",
e: &struct {
aa uint16
bb int
}{},
},
}
sort.Slice(data, func(i, j int) bool {
di := data[i]
dj := data[j]
return di.d*di.e.bb > dj.d*dj.e.bb
})
println("structs")
for _, d := range data {
println(d.name)
}
}
func main() {
strings()
int64s()
int32s()
int16s()
int8s()
ints()
structs()
}