Skip to content

Instantly share code, notes, and snippets.

@rupalbarman
Created April 5, 2017 02:48
Show Gist options
  • Select an option

  • Save rupalbarman/9ba342493d7812f1da0cbed648ccbf22 to your computer and use it in GitHub Desktop.

Select an option

Save rupalbarman/9ba342493d7812f1da0cbed648ccbf22 to your computer and use it in GitHub Desktop.
Sorting methods in GoLang
package main
import (
"fmt"
"sort"
)
func main() {
a:= []int{1,2,3,9,5,6}
fmt.Println(a)
fmt.Println(sort.SearchInts(a, 5))
person:= []struct{
name string
}{
{"rupal"},
{"barman"},
{"sxeli"},
}
fmt.Println(person)
sort.Slice(person, func(i, j int)bool { return person[i].name < person[j].name}) //second param is basically a Less() method
fmt.Println(person)
b:= [][]int{
[]int{1, 9},
[]int{3, 1},
}
fmt.Println(b)
sort.Slice(b, func(i, j int) bool { return b[i][1]< b[j][1]}) //sorting multi dimensional slices (defined in Less(), which is passed as a param
fmt.Println(b)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment