How to sort this without 'cmp=' in python 3?

sohcahtoa82 at gmail.com sohcahtoa82 at gmail.com
Fri Oct 14 19:53:37 EDT 2016


On Friday, October 14, 2016 at 4:35:08 PM UTC-7, 38016... at gmail.com wrote:
> nums=['3','30','34','32','9','5']
> I need to sort the list in order to get the largest number string: '953433230'
> 
> nums.sort(cmp=lambda a,b: cmp(a+b, b+a), reverse=True)
> 
> But how to do this in python 3?
> 
> Thank you

You don't need a lambda in this case.

Sort the strings in reverse order:
nums.sort(reverse=True)

Create a string:
biggestNum = ''.join(nums)

Or in a single line, which doesn't change the original value of nums:
biggestNum = ''.join(sorted(nums, reverse=True))




More information about the Python-list mailing list