DEV Community

Cover image for Sorting in Golang
bluepaperbirds
bluepaperbirds

Posted on

Sorting in Golang

Sorting numbers in Golang is nothing like C. If you don't know about the C language, it makes you write a sorting function yourself. It is much easier in Go. There's a package named sort.

This package has several methods to sort:

  • sort.Ints
  • sort.Float64s
  • sort.Strings

Use the method you need for the datatype.

Before trying this, make sure you can run Go programs.

Sort ints

This example shows you how to use the sort method for integers. Values are sorted from low to high.

package main

import (
        "fmt"
        "sort"
)

func main() {
        s := []int{5, 6, 3, 2, 1, 4} // unsorted
        sort.Ints(s)
        fmt.Println(s)
}

Save it and run:

$ go run ints.go 
[1 2 3 4 5 6]

Sort floats

If you have a bunch of floating points instead of integers, you can't use the Ints() method. But the same idea can be used for floats. You use another method Float64s().

package main

import (
    "fmt"
    "sort"
)

func main() {
    s := []float64{7.2, 3.3, 10.7, 13.8, 2.6} // unsorted
    sort.Float64s(s)
    fmt.Println(s)
}

Save and run:

$ go run floats.go
[2.6 3.3 7.2 10.7 13.8]

Related links:

Top comments (0)