Golang sort slice
Aug 26, 2017 · 1 min read
Starting Golang 1.8, this is one of the new way to sort slice available insort package
func Slice(slice interface{}, less func(i, j int) bool)This function take two arguments.
The first argument is the slice you want to sort. sort.Slicewill sort this slice argument in place. Note, this function doesn’t return anything, it will sort the slice in place.
The second argument is a func that will be used during the sort. The function signature is
less func(i, j int) boolThese two notes could be helpful when implementing this method.
iandjare indexes of the argumentslice. Element of indexicomes after the element of indexj. So when the function first got called,i = 1andj = 0. Note:iis afterjindex wise!- The function reports whether the element with index
ishould sort before the element with indexj. For example, if you want to sort the slice in ascending order, the function body will be something like
return slice[i].Stuff < slice[j].Stuff 