In various online discussions about the merits of Go, the pain of having to implement sort.Interface comes up, especially when one needs to sort a slice in multiple orders. Personally, implementing sort.Interface is no more than a buzz in my ear, but detractors tend to highlight it strongly anyway.
During one of these discussions I wrote up a pattern that allows you to sort using an anonymous less func. Of course, this comes with the downside of another indirection, and yet more type implementation, so I could only recommend this pattern if the performance impact is small and you need to sort in many (>3) orders and/or want to be able to quickly define new sorting orders.
The consuming syntax is actually pretty agreeable, and it comes with a feature to easily combine multiple orderings in a hierarchy. This isn’t extremely novel and I wouldn’t be surprised if I had actually seen this pattern elsewhere and since forgotten about it, but I thought I’d share anyway. Example usage:
var orderx datalessfn = func(a, b data) bool { return a.x < b.x }
sort.Sort(slice.By(orderx)) // sorts slice by data.x
ordery := func(a, b data) bool { return a.y < b.y }
sort.Sort(slice.By(orderx.Then(ordery))) // sorts slice by data.x, or when x is equal, then data.y
Thoughts?