How to sort a slice stable in Golang? - GeeksforGeeks

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 are allowed to sort a slice stable using SliceStable() function. This function sorts the specified slice given the specified less function while keeping the original order of equal elements. This function panics if the specified interface is not a slice. It is defined under the sort package so, you have to import sort package in your program for accessing SliceStable function.
Syntax:
func SliceStable(a_slice interface{}, less func(p, q int) bool)
Example:
// Go program to illustrate how// to sort a slice stablepackage main  import (    "fmt"    "sort")  // Main functionfunc 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},        {"Mina", 34, 109},        {"Cina", 634, 102},        {"Mina", 4, 100},        {"Rohit", 56, 1098},        {"Cina", 634, 102},        {"Mina", 39, 1098},        {"Sohit", 20, 110},    }      // Sorting Author by their names    // Using SliceStable() function    sort.SliceStable(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 the total articles    // Using SliceStable() function    sort.SliceStable(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 SliceStable() function    sort.SliceStable(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} {Cina 634 102} {Cina 634 102} {Mina 304 1098} {Mina 34 109} {Mina 4 100} {Mina 39 1098} {Rohit 56 1098} {Sohit 20 110} {Tina 104 105}]
Sort Author according to their total number of articles:[{Mina 4 100} {Sohit 20 110} {Mina 34 109} {Mina 39 1098} {Rohit 56 1098} {Tina 104 105} {Mina 304 1098} {Cina 634 102} {Cina 634 102} {Cina 634 102}]
Sort Author according to the their Ids:[{Mina 4 100} {Cina 634 102} {Cina 634 102} {Cina 634 102} {Tina 104 105} {Mina 34 109} {Sohit 20 110} {Mina 39 1098} {Rohit 56 1098} {Mina 304 1098}]
Golang
Golang-Slices
Go Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Parse JSON in Golang?
Defer Keyword in Golang
Time Durations in Golang
Anonymous function in Go Language
Loops in Go Language
time.Parse() Function in Golang With Examples
How to iterate over an Array using for loop in Golang?
Strings in Golang
Structures in Golang
Rune in Golang