Last active
April 22, 2019 00:31
-
-
Save tevin-morake/aba0d7b5bf9064a656b54fd505867a8a 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 Person struct { | |
First string | |
Age int | |
} | |
type ByName []Person | |
func (bn ByName) Len() int { return len(bn) } | |
func (bn ByName) Swap(i, j int) { bn[j], bn[i] = bn[i], bn[j] } | |
func (bn ByName) Less(i, j int) bool { return bn[i].Age < bn[j].Age } | |
func main() { | |
p1 := Person{"James", 32} | |
p2 := Person{"Moneypenny", 27} | |
p3 := Person{"Q", 64} | |
p4 := Person{"M", 56} | |
people := []Person{p1, p2, p3, p4} | |
fmt.Println(people) | |
sort.Sort(ByName(people)) | |
fmt.Println(people) | |
} | |
//output | |
//[{James 32} {Moneypenny 27} {Q 64} {M 56}] | |
//[{Moneypenny 27} {James 32} {M 56} {Q 64}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment