package main
import (
"fmt"
"sort"
)
type Person struct {
Name string
Age int
}
func main() {
// Step 1: Create an array of Person structs
people := []Person{
{"John", 30},
{"Alice", 25},
{"Bob", 35},
}
// Step 2: Define a custom type for sorting by Age
type ByAge []Person
// Step 3: Implement the sort.Interface - Len, Swap, and Less methods
func (a ByAge) Len() int { return len(a) }
func (a ByAge) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByAge) Less(i, j int) bool { return a[i].Age < a[j].Age }
// Step 4: Sort the array of Person structs using the custom sorting criteria
sort.Sort(ByAge(people))
// Step 5: Print the sorted array
fmt.Println("Sorted People by Age:", people)
}