Skip to content

Instantly share code, notes, and snippets.

@irskep
Created November 24, 2010 02:02
Show Gist options
  • Save irskep/712969 to your computer and use it in GitHub Desktop.
Save irskep/712969 to your computer and use it in GitHub Desktop.
Comparator for sorting a list of lists of strings
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