Sorting Slices In Go

Sorting Slices In Go

Learn How To Sort Slices in GoLang

In this short tutorial, we'll how to sort different types of slices in Go, and also, we will learn how to check if a slice is already sorted.

Is Slice Sorted

Before we learn to sort slices in Go we need to first learn to see if a slice is already sorted. The Go sort package provides us with a few functions to test if a slices is sorted or not.

package main

import (
	"fmt"
	"sort"
)

func main() {

	// Create slices.
	numbers := []int{44, 89, 5, 2, 25, 10}
	names := []string{"Mike", "Tom", "Adam", "Harry", "John"}
	grades := []float64{90.5, 88.4, 70.5, 100.0, 65.5, 43.5, 80.5, 90.5}
  
    // Is the int slice sorted?
	fmt.Println("Are numbers sorted?", sort.IntsAreSorted(numbers)) // Will print false.
    // Is the string slice sorted?
	fmt.Println("Are names sorted?", sort.StringsAreSorted(names)) // Will print false.
    // Is the float64 slice sorted?
	fmt.Println("Are grades sorted?", sort.Float64sAreSorted(grades)) // will print false.
}

Sorting a Slice of Ints

To sort a slice of ints, we use the Ints sort function.

package main

import (
	"fmt"
	"sort"
)

func main() {

	// Create slice of ints.
	numbers := []int{44, 89, 5, 2, 25, 10}
	
    // Is the int slice sorted?
    fmt.Println("Are numbers sorted?", sort.IntsAreSorted(numbers)) // Will print false.

    // Sort the int slice.
	sort.Ints(numbers)

    // Is the int slice sorted?
	fmt.Println("Are numbers sorted?", sort.IntsAreSorted(numbers)) // Will print true.

}

Sorting a Slice of Float64

To sort a slice of float64s, use the Float64s sort function.

package main

import (
	"fmt"
	"sort"
)

func main() {

	// Create slice of float64.
	grades := []float64{90.5, 88.4, 70.5, 100.0, 65.5, 43.5, 80.5, 90.5}
   
    // Is the float64 slice sorted? 
	fmt.Println("Are grades sorted?", sort.Float64sAreSorted(grades)) // will print false.
 
    // Sort the float64 slice.
	sort.Float64s(grades)

    // Is the float64 slice sorted? 
	fmt.Println("Are grades sorted?", sort.Float64sAreSorted(grades)) // will print true.
}

Sorting a Slice of Strings

To sort a slice of strings, we use the Strings sort function.

package main

import (
	"fmt"
	"sort"
)

func main() {

	// Create slice for strings.
	names := []string{"Mike", "Tom", "Adam", "Harry", "John"}
	
    // Is the string slice sorted?
	fmt.Println("Are names sorted?", sort.StringsAreSorted(names)) // Will print false.

    // Sort the string slice.
	sort.Strings(names)

    // Is the string slice sorted?
	fmt.Println("Are names sorted?", sort.StringsAreSorted(names)) // Will print true.
}

Conclusion

In this short tutorial, we'll learn how to sort different types of slices using Go and how to test if a slice is already sorted.

Did you like it? Why don't you try also...

Is String a Palindrome?

Is String a Palindrome?

Learn How to Test if a String is a Palindrome in Go.

How to Convert GoLang Integer to String Conversion

How to Convert GoLang Integer to String Conversion

Learn how to convert integers to strings and how to convert strings to integers in GoLang

Using Makefiles For Go (GoLang) Development

Using Makefiles For Go (GoLang) Development

Learn how to run, build and, compile Go (GoLang) Applications for any Platform and Operating System using a Makefile