Created
November 24, 2010 02:02
-
-
Save irskep/712969 to your computer and use it in GitHub Desktop.
Comparator for sorting a list of lists of strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import itertools | |
x = [['a', 'bcd'], ['ab', 'c']] | |
def cmp(a, b): | |
iter1 = itertools.chain(*a) | |
iter2 = itertools.chain(*b) | |
for ca, cb in itertools.izip(iter1, iter2): | |
if ca < cb: | |
return -1 | |
elif ca > cb: | |
return 1 | |
try: | |
iter2.next() | |
return -1 | |
except StopIteration: | |
try: | |
iter1.next() | |
return 1 | |
except StopIteration: | |
return 0 | |
print sorted(x, cmp=cmp) | |
print x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment