Skip to content

Instantly share code, notes, and snippets.

@kahunacohen
Created June 7, 2023 12:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kahunacohen/a0d2ae69b5328d07a512e339a8a7643b to your computer and use it in GitHub Desktop.
Save kahunacohen/a0d2ae69b5328d07a512e339a8a7643b to your computer and use it in GitHub Desktop.
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