Is there any way, given an object with the trait Ordered
, to get an Ordering
which uses the same comparison as the original object?
The reason I want to do this is I have implemented a merge sort with the following signature (the Manifest
s are just so that I can instantiate new List[T]
s and Array[T]
s:
def mergeSorted[T : Manifest](li: List[T], ord: Ordering[T]) : List[T]
What I want to do is overload this with a method with this signature:
def mergeSorted[T <: Ordered[T] : Manifest](li: List[T]) : List[T]
This way, I wouldn't have to manually, say, put in the Int
ordering if I were sorting Int
s. It seems to me that the easies way to implement this would just be to get an Ordering[T]
from T
somehow. The ScalaDoc seems to say that this is possible through implicits:
Ordered and Ordering both provide implicits allowing them to be used interchangeably.
However, I don't know which implicits to import in order to do this.