Sorting

You can order a slice with the help of the Slice() function. This function sorts the specified slice according to the provided less function. The result of this function is not stable.

So for stable sorting you can use SliceStable. And this function panics if the specified interface is not of type slice.


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

Furthermore, we can use the built-in sort package to sort a slice instead of manually writing any sorting algorithms.


// Sort slice of type []string in alphabetical order
sort.Strings

// Sort slice of type []string in inverted alphabetical order
sort.Sort(sort.Reverse(sort.String(slice)))

                        
sorting.go