How to sort list

Klaus Alexander Seistrup klaus at seistrup.dk
Tue Nov 21 14:23:36 EST 2006


Lad <python at hope.cz> wrote:

> I have a list of emails and I would like to sorted that list by 
> domains
> E.g.
> If the list is
>
> Emails=['a at hotmail.com','a at yahoo.com','b at hotmail.com','c at yahoo.com',....]
>
> after sorting I would like to have
> Emails=['a at hotmail.com','b at hotmail.com','a at yahoo.com','c at yahoo.com',....]
>
> What is the best/easiest way?

Decorate-sort-undecorate?

#v+

array = []

for addr in Emails:
  (user, domain) = addr.split('@')
  array.append((domain, user, addr))
# end for

array.sort()

SortedEmails = [addr for (user, domain, addr) in array]

#v-

Cheers,

-- 
Klaus Alexander Seistrup
http://klaus.seistrup.dk/



More information about the Python-list mailing list