Код: Выделить всё
from functools import cmp_to_key
def compare(a,b):
print(a,b, a+b,b+a,a+b>b+a)
return (a+b)>(b+a) # first comparing function
def sortArr(ls):
ls = sorted(ls,key =cmp_to_key(compare),reverse=True)
return "".join(ls)
ls = ["8","81","82","829"]
print(sortArr(ls))
Код: Выделить всё
82 829 82829 82982 False
81 82 8182 8281 False
8 81 881 818 True
88182829
Код: Выделить всё
from functools import cmp_to_key
def compare(a,b):
print(a,b, a+b,b+a,a+b>b+a)
return int(a+b) -int(b+a) # second comparing function
def sortArr(ls):
ls = sorted(ls,key =cmp_to_key(compare),reverse=True)
return "".join(ls)
ls = ["8","81","82","829"]
print(sortArr(ls))
Код: Выделить всё
82 829 82829 82982 False
81 82 8182 8281 False
8 81 881 818 True
8 82 882 828 True
8 829 8829 8298 True
88298281
Так почему же первая функция сравнения не работала?
Подробнее здесь: https://stackoverflow.com/questions/790 ... -some-comp