Last active
January 17, 2022 13:28
-
-
Save pkbhowmick/20f04bc5c81c23b9d561f4bb8c0dbcb2 to your computer and use it in GitHub Desktop.
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" | |
) | |
type Person struct { | |
name string | |
weight float64 | |
} | |
func getRandomPersons() []Person { | |
p1 := Person{ | |
name: "azz", | |
weight: 10.4, | |
} | |
p2 := Person{ | |
name: "abc", | |
weight: 18.5, | |
} | |
p := make([]Person, 0) | |
p = append(p, p1, p2) | |
return p | |
} | |
func main() { | |
persons := getRandomPersons() | |
sort.Slice(persons, func(i, j int) bool { | |
return persons[i].name < persons[j].name | |
}) | |
fmt.Println(persons) | |
sort.Slice(persons, func(i, j int) bool { | |
return persons[i].weight < persons[j].weight | |
}) | |
fmt.Println(persons) | |
} | |
// Output: | |
// [{abc 18.5} {azz 10.4}] | |
// [{azz 10.4} {abc 18.5}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment