Created
June 7, 2023 12:14
-
-
Save kahunacohen/a0d2ae69b5328d07a512e339a8a7643b to your computer and use it in GitHub Desktop.
This file contains 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 AgeFactor []Person | |
func (a AgeFactor) Len() int { return len(a) } | |
func (a AgeFactor) Less(i, j int) bool { return a[j].age < a[i].age } | |
func (a AgeFactor) Swap(i, j int) { a[i], a[j] = a[j], a[i] } | |
type Person struct { | |
age int | |
firstName string | |
lastName string | |
} | |
func main() { | |
people := []Person{Person{age: 10, firstName: "Reuben", lastName: "Cohen"}, Person{firstName: "Aaron", lastName: "Cohen", age: 50}, Person{firstName: "Courtney", lastName: "Cohen", age: 46}} | |
sort.Sort(AgeFactor(people)) | |
for _, p := range people { | |
fmt.Printf("%s %s is %d years old\n", p.firstName, p.lastName, p.age) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment