Closed stephenh closed 9 years ago
I guess the standard way to sort by a function in Java 8 is to use:
seq(o1, o2).sorted(Comparator.comparing(o -> o.getName()))
I agree though, that a shortcut for the above as you suggested would be rather useful. I'll pull, but rename the method to the expected sorted()
, to lessen the surprise for someone discovering the API
Thanks! You're right that it's just a short cut.
FWIW Scala's Seq has Seq.sorted and Seq.sortBy as different method names, and I kind of like how the "sort by (function)" reads in my head, but sorted is perfectly fine too. I'll get used to it. :-)
Well, same story as over here: https://github.com/jOOQ/jOOL/issues/93#issuecomment-122901666. Besides (being a SQL person), the only reasonable reading would be ORDER BY
anyway... ;)
the only reasonable reading would be ORDER BY anyway... ;)
Of course. :-)
Currently sorting objects by an attribute can be done by, e.g. sorting by name:
Scala has a nice
sortBy
function that avoids calling .getName twice:Where the lambda returns a Comparable, so the .sortBy can do the .compareTo as needed.