0

What is the difference between Ordering.by() and Ordering.on() in Scala?

(Well, one is defined in the trait and the other in the companion object, and the implementation code is also different, but there must be a reason why the language needs both.)

1
  • 2
    Well, as you said, because one is in the object thus is a factory, and the other in the trait, thus being a combinator. They are both there to help you create newer Ordering instances out of existing ones. Commented Oct 12, 2022 at 19:48

1 Answer 1

3

From the documentation of Ordering.by:

This function is an analogue to Ordering.on where the Ordering[S] parameter is passed implicitly.

Basically the two are the same, it's just a matter of convenience depending of what is your context and what you're working with.

Sign up to request clarification or add additional context in comments.

2 Comments

Can you please provide examples? In what case what is more convenient?
@18446744073709551615 if you want to create a new ordering out of nowhere, then something like Ordering.by((foo: Foo) => ...) is very convenient, more than doping Ordering[Bar].on(foo => ...) - However, in some other contexts it may be more convenient to call on on an existing instance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.