Skip to content
Related Articles

Related Articles

How to sort a slice in Golang?
  • Last Updated : 28 Aug, 2019

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 sort a slice with the help of Slice() function. This function sorts the specified slice given the provided less function. The result of this function is not stable. So for stable sort, you can use SliceStable. And this function panics if the specified interface is not of slice type. It is defined under the sort package so, you have to import sort package in your program for accessing Slice function.

Syntax:

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

Example:




// Go program to illustrate
// how to sort a slice
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 })
      
    fmt.Println("Sort Author according to their names:")
    fmt.Println(Author)
  
    // Sorting Author by their
    // total number of articles
    // Using Slice() function
    sort.Slice(Author, func(p, q int) bool
       return Author[p].a_article < Author[q].a_article })
      
    fmt.Println()
    fmt.Println("Sort Author according to their"+
                    " total number of articles:")
      
    fmt.Println(Author)
  
    // 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 })
      
    fmt.Println()
    fmt.Println("Sort Author according to the their Ids:")
    fmt.Println(Author)
}

Output:

Sort Author according to their names:
[{Cina 634 102} {Mina 304 1098} {Mohit 300 104} {Rina 10 108} {Riya 4 101} {Rohit 56 107} {Sina 234 103} {Sohit 20 110} {Tina 104 105} {Vina 237 106}]

Sort Author according to their total number of articles:
[{Riya 4 101} {Rina 10 108} {Sohit 20 110} {Rohit 56 107} {Tina 104 105} {Sina 234 103} {Vina 237 106} {Mohit 300 104} {Mina 304 1098} {Cina 634 102}]

Sort Author according to the their Ids:
[{Riya 4 101} {Cina 634 102} {Sina 234 103} {Mohit 300 104} {Tina 104 105} {Vina 237 106} {Rohit 56 107} {Rina 10 108} {Sohit 20 110} {Mina 304 1098}]




My Personal Notes arrow_drop_up
Recommended Articles
Page :