I am trying to write a function that given a list of non negative integers, arranges them such that they form the largest possible number.
For example, given
The most straight-forward way is to use itertools.permutations() to model how you would solve this by hand:
>>> from itertools import permutations, imap
>>> a = [50, 2, 1, 9]
>>> int(max(imap(''.join, permutations(map(str, a)))))
95021
import functools
def cmpr(x, y):
xy = str(x) + str(y)
yx = str(y) + str(x)
return -1 if (xy > yx) else 1
a = [50, 2, 1, 9]
a.sort(key=functools.cmp_to_key(cmpr))