Check if the given slice is sorted in Golang

In Go language slice is more powerful, flexible, convenient than an array, and is a lightweight data structure. The slice is a variable-length sequence which stores elements of a similar type, you are not allowed to store different type of elements in the same slice.
In Go language, you can check the given slice is sorted or not with the help of SliceIsSorted() function. This function returns true if the given slice is sorted. Or return false if the given slice is not sorted. And this function will panic if the specified interface is not a slice type. It is defined under the sort package so, you have to import sort package in your program for accessing SliceIsSorted function.

Syntax:

func SliceIsSorted(a_slice interface{}, less func(p, q int) bool) bool

Example:



filter_none

edit
close

play_arrow

link
brightness_4
code

// Go program to illustrate how to check
// the given slice is sorted or not
package main
  
import (
    "fmt"
    "sort"
)
  
// Main function
func main() {
  
    // Creating and initializing
    // a structure
    Author := []struct {
        a_name    string
        a_article int
        a_id      int
    }{
        {"Mina", 304, 1098},
        {"Cina", 634, 102},
        {"Tina", 104, 105},
        {"Rina", 10, 108},
        {"Sina", 234, 103},
        {"Vina", 237, 106},
        {"Rohit", 56, 107},
        {"Mohit", 300, 104},
        {"Riya", 4, 101},
        {"Sohit", 20, 110},
    }
  
    // Sorting Author by their name
    // Using Slice() function
    sort.Slice(Author, func(p, q int) bool
     return Author[p].a_name < Author[q].a_name })
  
    // Checking the slice is sorted
    // according to their names
    // Using SliceIsSorted function
    res1 := sort.SliceIsSorted(Author, func(p, q int) bool
               return Author[p].a_name < Author[q].a_name })
      
    if res1 == true {
      
        fmt.Println("Slice is sorted by their names")
          
    } else {
      
        fmt.Println("Slice is not sorted by their names")
    }
  
    // Checking the slice is sorted 
    // according to their total articles
    // Using SliceIsSorted function
    res2 := sort.SliceIsSorted(Author, func(p, q int) bool
         return Author[p].a_article < Author[q].a_article })
      
    if res2 == true {
      
        fmt.Println("Slice is sorted by "+
         "their total number of articles")
          
    } else {
      
        fmt.Println("Slice is not sorted by"+
           " their total number of articles")
    }
  
    // Sorting Author by their ids
    // Using Slice() function
    sort.Slice(Author, func(p, q int) bool
      return Author[p].a_id < Author[q].a_id })
  
    // Checking the slice is sorted
    // according to their ids
    // Using SliceIsSorted function
    res3 := sort.SliceIsSorted(Author, func(p, q int) bool
                   return Author[p].a_id < Author[q].a_id })
      
    if res3 == true {
      
        fmt.Println("Slice is sorted by their ids")
          
    } else {
      
        fmt.Println("Slice is not sorted by their ids")
    }
}

chevron_right


Output:

Slice is sorted by their names
Slice is not sorted by their total number of articles
Slice is sorted by their ids


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.