Created
April 5, 2017 02:48
-
-
Save rupalbarman/9ba342493d7812f1da0cbed648ccbf22 to your computer and use it in GitHub Desktop.
Sorting methods in GoLang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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