-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
30 lines (26 loc) · 790 Bytes
/
Copy pathmain.go
File metadata and controls
30 lines (26 loc) · 790 Bytes
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
// The `so/slices` package implements sorting for builtins and
// user-defined types. We'll look at sorting for builtins first.
package main
import (
"solod.dev/so/fmt"
"solod.dev/so/slices"
)
func main() {
// Sorting functions are generic, and work for any
// ordered built-in type. For a list of ordered
// types, see `cmp.Ordered`.
strs := []string{"c", "a", "b"}
slices.Sort(strs)
fmt.Printf("Strings: [%s %s %s]\n", strs[0], strs[1], strs[2])
// An example of sorting `int`s.
ints := []int{7, 2, 4}
slices.Sort(ints)
fmt.Printf("Ints: [%d %d %d]\n", ints[0], ints[1], ints[2])
// We can also use the `slices` package to check
// if a slice is already in sorted order.
if slices.IsSorted(ints) {
fmt.Println("Sorted!")
} else {
fmt.Println("Not sorted :(")
}
}