Ticket #6580: by_name.py

File by_name.py, 420 bytes (added by Lukasz Dobrzanski, 8 years ago)

sorting using key only

Line 
1# sorted andsoring in general makes cmp= exitinct
2# http://wiki.python.org/moin/HowTo/Sorting/
3
4def by_name(n):
5    rest, delim, suffix = n.rpartition('.')
6    rest_parts = rest.split('.')
7    return tuple( reversed(rest_parts + [-len(rest_parts)] + [suffix]) )
8
9>>> sorted(['a.b.com', 'a.b.c.com', 'com', 'zzz.b.com', 'd.net', 'a.b.net'], key=by_name)
10['a.b.c.com', 'a.b.com', 'zzz.b.com', 'com', 'a.b.net', 'd.net']