The easiest way is to define an implicit Ordering[T] on them, but then you have to pass this ordering to the sort function (or any other function that wants to compare them). It is also possible to pass it implicitly.
Another way would be, to extend the tuple class by the < operator via an implicit cast:
implicit def compareTuple[T](lhs: (T,T)) = new {
def <(rhs: (T,T)) = lhs._1<rhs._1 || (lhs._1==rhs._1 && lhs._2<rhs._2)
}
edit:
If you want to have the other comparison operators too, you could get them by inheriting from Ordered[T]:
implicit def compareTuple[T](lhs: (T,T)) = new Ordered[(T,T)] {
def compare(rhs: (T,T)) = ...
}
edit2:
If you also need to compare tuples of different sizes, you can use the productIterator function which is defined in all tuple classes (see documentation) and allows you to get an iterator over the tuple. This way you could write a function like you would do it with a list.
edit3:
This would be something like:
implicit def compareTuple[T <: Product](lhs: T) = new Ordered[T] {
def compare[U <: Product](rhs: U) = {
def compare(lhs: Any, rhs: Any) = ...
def iteratorCompare(lhs: Iterator[Any], rhs: Iterator[Any]):Int =
if(!lhs.hasNext)
if(!rhs.hasNext)
0
else
-1
else if(!rhs.hasNext)
1
else
compare(lhs.next,rhs.next)
iteratorCompare(lhs.productIterator,rhs.productIterator)
}
}
But with this approach you have to take care about the types. Because the function doesn't know the types of the tuple elements (they can be different inside of the same tuple), it can only provide you an Iterator[Any]. So you have to define a compare(Any,Any) function to handle what you want.