Reputation: 81
I have a list of tuples, each containing a number and a string, at the moment I am sorting them by the number contained within them, however I am wondering if there is a way to sort them alphabetically in case the numbers within multiple tuples are equal.
Upvotes: 2
Views: 309
Reputation: 139840
The Ord
instance for tuples already works like that (compare first items first, if equal compare the next ones, and so on), so you can simply use sort
.
> Data.List.sort [(3, "foo"), (1, "bar"), (2, "xyzzy"), (2, "baz")]
[(1,"bar"),(2,"baz"),(2,"xyzzy"),(3,"foo")]
Upvotes: 10