Solution Review: Sort People with Sorter Interface
Explore how to implement the Sorter interface to sort a slice of Person structs by their full name. Understand defining sorting methods Len, Less, and Swap, and apply them to organize data in Go. This lesson helps you grasp practical interface use for sorting operations.
We'll cover the following...
We'll cover the following...
package mysort
type Interface interface {
Len() int
Less(i, j int) bool
Swap(i, j int)
}
func Sort(data Interface) {
for pass:=1; pass < data.Len(); pass++ {
for i:=0; i < data.Len() - pass; i++ {
if data.Less(i+1, i) {
data.Swap(i, i+1)
}
}
}
}
func IsSorted(data Interface) bool {
n := data.Len()
for i := n - 1; i > 0; i-- {
if data.Less(i, i-1) {
return false
}
}
return true
}
// Convenience types for common cases
type IntSlice []int
func (p IntSlice) Len() int { return len(p) }
func (p IntSlice) Less(i, j int) bool { return p[i] < p[j] }
func (p IntSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
type StringSlice []string
func (p StringSlice) Len() int { return len(p) }
func (p StringSlice) Less(i, j int) bool { return p[i] < p[j] }
func (p StringSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
// Convenience wrappers for common cases
func SortInts(a []int) { Sort(IntSlice(a)) }
func SortStrings(a []string) { Sort(StringSlice(a)) }
func IntsAreSorted(a []int) bool { return IsSorted(IntSlice(a)) }
func StringsAreSorted(a []string) bool { return IsSorted(StringSlice(a)) }From line 8 to line 11, we define a struct Person with two fields: firstName and LastName. Then at line 13 we define a type Persons as a [ ]Person.
In order to be able to sort Persons, we need to implement the Sorter interface, which is defined in mysort.go- in the mysort ...