Overview
Sort a product list in Go with sort.Slice, cheapest first and equal prices by name, because sort.Slice is not stable and won't keep ties in order for you.
- sort.Slice(s, less) sorts in place
- less(i, j): does i go first?
- break ties yourself
Short Course
sort.Slice(s, less) sorts in place; less(i, j) says whether item i goes
first. Trap: sort.Slice isn't stable, so equal items may come out in any
order. Break ties in less.
sort.Slice(ps, func(i, j int) bool {
return ps[i].Price < ps[j].Price
})
Task
Write SortProducts(ps []Product): sort in place, cheapest first, equal
prices in alphabetical order of name. Example: {tea 3} {cake 5} {coffee 3} →
{coffee 3} {tea 3} {cake 5}.
Test Suites
Pending
Next
Output