Sort by domain name?

Paul McGuire ptmcg at austin.rr._bogus_.com
Mon Oct 2 12:11:22 EDT 2006


"js " <ebgssth at gmail.com> wrote in message 
news:mailman.1094.1159801691.10491.python-list at python.org...
> Hi list,
>
> I have a list of URL and I want to sort that list by the domain name.
>
> Here, domain name doesn't contain subdomain,
> or should I say, domain's part of 'www', mail, news and en should be 
> excluded.
>
> For example, if the list was the following
> ------------------------------------------------------------
> http://mail.google.com
> http://reader.google.com
> http://mail.yahoo.co.uk
> http://google.com
> http://mail.yahoo.com
> ------------------------------------------------------------
>
> the sort's output would be
> ------------------------------------------------------------
> http://google.com
> http://mail.google.com
> http://reader.google.com
> http://mail.yahoo.co.uk
> http://mail.yahoo.com
> ------------------------------------------------------------
>
> As you can see above, I don't want to
>
>
> Thanks in advance.

How about sorting the strings as they are reversed?

urls = """\
http://mail.google.com
http://reader.google.com
http://mail.yahoo.co.uk
http://google.com
http://mail.yahoo.com""".split("\n")

sortedList = [ su[1] for su in sorted([ (u[::-1],u) for u in urls ]) ]

for url in sortedList:
    print url


Prints:
http://mail.yahoo.co.uk
http://mail.google.com
http://reader.google.com
http://google.com
http://mail.yahoo.com


Close to what you are looking for, might be good enough?

-- Paul 





More information about the Python-list mailing list