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

breamoreboy at gmail.com breamoreboy at gmail.com
Sat Oct 15 00:04:45 EDT 2016


On Saturday, October 15, 2016 at 12:53:48 AM UTC+1, sohca... at gmail.com wrote:
> 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))

No, you've made exactly the same mistake that I did :(

>>> nums=['3','30','34','32','9','5']
>>> wants='953433230'
>>> nums.sort(reverse=True)
>>> result = ''.join(nums)
>>> result == wants
False
>>> result
'953432303'



More information about the Python-list mailing list