Ted West
Ted West

Reputation: 81

Sorting tuples alphabetically

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

Answers (1)

hammar
hammar

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

Related Questions