English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Golang Basic Tutorial

Golang Control Statements

Golang Function & Method

Golang Struct

Golang Slice & Array

Golang String(String)

Golang Pointer

Golang Interface

Golang Concurrency

Golang Exception(Error)

Golang Miscellaneous

Go Language Slice Sorting

In Go language,SliceThanArrayMore powerful, flexible, convenient, and lightweight data structure. Slice is a variable-length sequence used to store elements of similar types, and it does not allow different types of elements to be stored in the same slice.
Go language allows you to sort the elements of a slice based on the type of the slice. Therefore, you can sort int type slices using the following functions. These functions are defined under the sort package, so you must import the sort package in your program to access these functions:

1.Integers:This function is used only to sort integer slices and sort the elements in the slice in ascending order.

Syntax:

func Ints(slc []int)

Here,slcrepresents an integer. Let's discuss this concept with an example:

//Sort integers
package main 
   
import ( 
    "fmt"
    "sort"
) 
   
// Main function 
func main() { 
       
    //Create and initialize slice using shorthand declaration
    scl1 := []int{400, 600, 100, 300, 500, 200, 900} 
    scl2 := []int{-23, 567, -34, 67, 0, 12, -5} 
       
    //Display slices
    fmt.Println("Slices(Before):") 
    fmt.Println("Slice" 1: ", scl1) 
    fmt.Println("Slice" 2: ", scl2) 
       
    //Sort integer slices
//Using Ints function
    sort.Ints (scl1) 
    sort.Ints (scl2) 
       
    //Display result
    fmt.Println("\nSlices(After):") 
    fmt.Println("Slice" 1 : ", scl1) 
    fmt.Println("Slice" 2 : ",scl2) 
}

Output:

Slices(Before):
Slice 1:  [400 600 100 300 500 200 900]
Slice 2:  [-23 567 -34 67 0 12 -5]
Slices(After):
Slice 1 :  [100 200 300 400 500 600 900]
Slice 2 :  [-34 -23 -5 0 12 67 567]

2. IntsAreSorted:This function is used to check if the given int slice is sorted in ascending order. If the slice is sorted, this method returns true; otherwise, if the slice is not sorted, it returns false.

Syntax:

func IntsAreSorted(scl []int) bool

Here,sclrepresentspart of ints.Let's discuss this concept with an example:

//Go program explains how to check
//Is the given int slice sorted
package main
import (
    "fmt"
    "sort"
)
func main() {
    //Create and initialize slice
    //Using shorthand declaration
    scl1 := []int{100, 200, 300, 400, 500, 600, 700}
    scl2 := []int{-23, 567, -34, 67, 0, 12, -5}
    //Display slices
    fmt.Println("Slices:")
    fmt.Println("Slice"1: ", scl1)
    fmt.Println("Slice"2: ", scl2)
    //Check if the slice is sorted
    //Using IntsAreSorted function
    res1 := sort.IntsAreSorted(scl1)
    res2 := sort.IntsAreSorted(scl2)
    //Display result
    fmt.Println("\nResult:")
    fmt.Println("Slice"1Is it sorted?: ", res1)
    fmt.Println("Slice"2Is it sorted?: ", res2)
}

Output:

Slices:
Slice1:  [100 200 300 400 500 600 700]
Slice2:  [-23 567 -34 67 0 12 -5]
Result:
Slice1Is it sorted?:  true
Slice2Is it sorted?:  false