GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

No menu could be recovered. Follow the blue links in the page.

This view

  • Original page
  • New address

GeezerVison is showing gobyexample.com — Go by Example: Sorting

Go by Example: Sorting

Go’s slices package implements sorting for builtins and user-defined types. We’ll look at sorting for builtins first.

package main
import (
    "fmt"
    "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.Println("Strings:", strs)

An example of sorting ints.

    ints := []int{7, 2, 4}
    slices.Sort(ints)
    fmt.Println("Ints:   ", ints)

We can also use the slices package to check if a slice is already in sorted order.

    s := slices.IsSorted(ints)
    fmt.Println("Sorted: ", s)
}
$ go run sorting.go
Strings: [a b c]
Ints:    [2 4 7]
Sorted:  true

Next example: Sorting by Functions.

by Mark McGranaghan and Eli Bendersky | source | license

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.