How to check if an array of int values are sorted in Golang

Last updated on Jun 23, 2021 by Suraj Sharma



In this tutorial, you will learn to use the IntsArtSorted function of the Golang package sort to check if an array of int values are sorted in increasing order.

The IntsAreSorted function checks whether a slice of int values are sorted in increasing order or not. It returns a bool value

sort.IntsAreSorted accepts a slice of int type as the only argument.

func IntsAreSorted(arr []int) bool


Check if an array of int values are sorted


package main

import (
    "fmt"
    "sort"
)

func main () {
    unSortedArray := []int{2,1,5,3,11}

    increasingArray := []int{1,2,3,4,5,5} // increasing order

    descArray := []int{6,5,4,3,2,1} // descending order


    fmt.Println(sort.IntsAreSorted(unSortedArray)) // false

    fmt.Println(sort.IntsAreSorted(increasingArray)) // true

    fmt.Println(sort.IntsAreSorted(descArray)) // false
}


Related Solutions


Rate this post


Suraj Sharma is the founder of Future Gen AI Services. He holds a B.Tech degree in Computer Science & Engineering from NIT Rourkela.