So i was solving this question -
https://leetcode.com/problems/largest-number/ and found this great answer but can't able to understand cmp_to_key() . I try to debug but still no clue how its working?
from functools import cmp_to_key
class Solution:
def largestNumber(self, nums: List[int]) -> str:
def cmp_func(x, y):
"""Sorted by value of concatenated string increasingly."""
print(x,y)
if x + y > y + x:
return 1
elif x == y:
return 0
else:
return -1
# Build nums contains all numbers in the String format.
nums = [str(num) for num in nums]
print(nums)
# Sort nums by cmp_func decreasingly.
nums.sort(key = cmp_to_key(cmp_func), reverse = True)
# Remove leading 0s, if empty return '0'.
return ''.join(nums).lstrip('0') or '0
```'
Input: nums = [3,30,34,5,9]
stdout
['3', '30', '34', '5', '9']
5 9
34 5
30 34
3 30
3 5
3 34
3 30
Output
"9534330"
Expected
"9534330"