Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Sort slice

  • sort

  • sort.Strings

  • sort.Ints

  • sort

package main

import (
	"fmt"
	"sort"
)

func main() {
	dwarfs := []string{"Doc", "Grumpy", "Happy", "Sleepy", "Bashful", "Sneezy", "Dopey"}
	fmt.Println(dwarfs)

	sort.Strings(dwarfs)
	fmt.Println(dwarfs)

	scores := []int{17, 3, 42, 28}
	fmt.Println(scores)

	sort.Ints(scores)
	fmt.Println(scores)
}
[Doc Grumpy Happy Sleepy Bashful Sneezy Dopey]
[Bashful Doc Dopey Grumpy Happy Sleepy Sneezy]
[17 3 42 28]
[3 17 28 42]