1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| package main
import ( "cmp" "fmt" "slices" )
func main() { type Order struct { Product string Customer string Price float64 } orders := []Order{ {"foo", "alice", 1.00}, {"bar", "bob", 3.00}, {"baz", "carol", 4.00}, {"foo", "alice", 2.00}, {"bar", "carol", 1.00}, {"foo", "bob", 4.00}, } slices.SortFunc(orders, func(a, b Order) int { return cmp.Or( cmp.Compare(a.Customer, b.Customer), cmp.Compare(a.Product, b.Product), cmp.Compare(b.Price, a.Price), ) }) for _, order := range orders { fmt.Printf("%s %s %.2f\n", order.Product, order.Customer, order.Price) }
}
|