The sort Package
sort.Ints, sort.Strings and sort.Slice order data. slices.Sort is the generic version.
Example
package main
import (
"fmt"
"sort"
)
func main() {
a := []int{5, 2, 9}
sort.Ints(a)
p := []string{"pear", "fig"}
sort.Slice(p, func(i, j int) bool { return len(p[i]) < len(p[j]) })
fmt.Println(a, p)
}Result
[2 5 9] [fig pear]
Exercise
Sort by rule.
sort.(p, less)
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.