March 2019
Intermediate to advanced
336 pages
9h 9m
English
The SortByAge method sorts the elements concerned by Age. The SortByAge interface operates on the Employee array. This is shown in the following code snippet:
// SortByAge typetype SortByAge []Employee// SortByAge interface methodsfunc (sortIntf SortByAge) Len() int { return len(sortIntf) }func (sortIntf SortByAge) Swap(i int, j int) { sortIntf[i], sortIntf[j] = sortIntf[j], sortIntf[i] }func (sortIntf SortByAge) Less(i int, j int) bool { return sortIntf[i].Age < sortIntf[j].Age }
The main method initializes the employees array and sorts the array by age:
func main() { var employees = []Employee{ {"Graham","231",235643,31}, {"John", "3434",245643,42}, {"Michael","8934",32432, 17}, {"Jenny", "24334",32444,26}, } fmt.Println(employees) ...
Read now
Unlock full access