[Tutor] sorting tuples

Alexandre Ratti alex@gabuzomeu.net
Mon, 20 May 2002 18:42:39 +0200


Hello Maximiliano,


At 11:46 20/05/2002 -0400, you wrote:
>Date: Mon, 20 May 2002 06:55:47 -0700 (PDT)
>From: Maximiliano Ichazo <max_ig@yahoo.com>
>Subject: [Tutor] sorting tuples

>I have a tuple with tuples in it (these sub-tuples have three items
>each). I want to sort the items of the main tuple based on the first
>item of the sub-tuples. However I don't know how can I do this.

You could convert the tuple to a list, and then sort the list. Try this:

toto = (('b', 'c', 'a'), ('d', 'e', 'e'), ('a','b','c'))
titi = list(toto)
titi.sort()
print titi
 >>> [('a', 'b', 'c'), ('b', 'c', 'a'), ('d', 'e', 'e')]

To convert the list back to a tuple, use "tuple(theList)".


Cheers.

Alexandre