How to use sorted using key=lambda instead of cmp_to_key?

55 Views Asked by At

Given an array A of non-negative integers, arrange them such that they form the largest number. example :

 A = [3, 30, 34, 5, 9]
 output : "9534330"

I am able to do this using custom compare function, is it possible to achieve the same using lambda?

Code:

from functools import cmp_to_key

A = list(map(str,A))

def compare(num1,num2):
    if num1 + num2 > num2 + num1:
        return -1           
    else:
        return 1

A = sorted(A,key=cmp_to_key(compare))
return "".join(A)
0

There are 0 best solutions below