Sorting email addresses by domain

EP EP at zomething.com
Wed Nov 10 22:12:38 EST 2004


From: Greg Krohn @ invalid wrote:

> Elegant or Perlish, you decide:
> 
> addresses.sort(lambda a, b: cmp(a[a.find('@'):].lower(),
>                                  b[b.find('@'):].lower()))
> 
> Note that this isn't case sensitive and items without an '@' will be 
> placed at the end.


I think: yes and yes.  Looks efficient versus what came to mind for me:

>>> def sortByDomain(addies=[]):
	domains=[a.split("@")[1] for a in addies]
	aPairs=zip(domains,addies)
	aPairs.sort()
	sortedAddies=[tup[1] for tup in aPairs]
	return sortedAddies


I do like having functions with obvious names so that I don't have to trace the logic to remember what they do...




More information about the Python-list mailing list