jOOQ / jOOL

jOOλ - The Missing Parts in Java 8 jOOλ improves the JDK libraries in areas where the Expert Group's focus was elsewhere. It adds tuple support, function support, and a lot of additional functionality around sequential Streams. The JDK 8's main efforts (default methods, lambdas, and the Stream API) were focused around maintaining backwards compatibility and implementing a functional API for parallelism.
http://www.jooq.org/products
Apache License 2.0
2.09k stars 168 forks source link

Add Seq.sorted(Function) #96

Closed stephenh closed 9 years ago

stephenh commented 9 years ago

Currently sorting objects by an attribute can be done by, e.g. sorting by name:

   Seq(o1, o2).sorted((o1, o2) -> o1.getName().compareTo(o2.getName()))

Scala has a nice sortBy function that avoids calling .getName twice:

   Seq(o1, o2).sortBy(o -> o.getName());

Where the lambda returns a Comparable, so the .sortBy can do the .compareTo as needed.

lukaseder commented 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

stephenh commented 9 years ago

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. :-)

lukaseder commented 9 years ago

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... ;)

stephenh commented 9 years ago

the only reasonable reading would be ORDER BY anyway... ;)

Of course. :-)