Files
go_playground/5-go-by-example/41-sorting.go
2022-01-12 19:22:31 +01:00

27 lines
607 B
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package main
import (
"fmt"
"sort"
)
// Gos sort package implements sorting for builtins and user-defined types
func main() {
// Sort methods are specific to the builtin type
// an example for strings
// sorting is in-place, so it changes the given slice and doesnt return a new one
strs := []string{"c", "a", "b"}
sort.Strings(strs)
fmt.Println("Strings:", strs)
// an example of sorting ints
ints := []int{7, 2, 4}
sort.Ints(ints)
fmt.Println("Ints: ", ints)
// use sort to check if a slice is already in sorted order
s := sort.IntsAreSorted(ints)
fmt.Println("Sorted: ", s)
}