list.sortBy(_.stuff)

61 views
Skip to first unread message

Dennis Haupt

unread,
Aug 25, 2016, 7:28:05 AM8/25/16
to scala-user
hi all,

i read some library code and realized that in Ordering.by, the ordering agoithm used is based on "lessThan" which is less efficient than using a comparator directly. why is that?

for example, if i want to sort a list of ints, instead of using
Integer.compare(a,b) scala actually does:

if (a lessThan b) -1 else if (b lessThan a) 1 else 0

won't this make the whole process unnecessarily slower? if i already have an implicit ordering on the type i want to order on, doesn't it imply i already have a comparator?

Seth Tisue

unread,
Aug 29, 2016, 7:06:03 PM8/29/16
to scala-user
On Thu, Aug 25, 2016 at 4:27 AM, Dennis Haupt <d.ha...@gmail.com> wrote:
won't this make the whole process unnecessarily slower?

It looks that way to me. If you make the change and it compiles and the test suite still passes, consider submitting it as a pull request.

(I see that the implementation in terms of fromLessThan was originally suggested by Daniel Sobral at https://issues.scala-lang.org/browse/SI-3155)

Seth Tisue / Scala team / Lightbend, Inc.

Naftoli Gugenheim

unread,
Sep 1, 2016, 5:36:25 AM9/1/16
to Seth Tisue, scala-user

It doesn't seem like there's a factory it could use, but you could just subclass Ordering.

def by[A, B:Ordering](f:A=>B) = new Ordering {
  def compare ...

Actually that really exists as Ordering#on. So, simply def by[A, B:Ordering](f:A=>B) = Ordering[B].on[A](f) should do it.


--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages