Reputation: 43
I have a list
codelist=['11CE711-141', '11CS710-142', '11EC710-142', '11EE610-8', '11EEP60-5', '11IT610-1', '11ITRA0-66', '11ME710-141', '11MT710-71']
I want to sorted list in descending order based on numerical value looks like below
codelist=['11CS710-142','11EC710-142','11CE711-141','11ME710-141','11MT710-71','11ITRA0-66','11EE610-8','11EEP60-5','11IT610-1']
Kindly suggest me ideas to resolve this issue.Thanks
Upvotes: 2
Views: 57
Reputation: 106995
You can sort with a key function that returns the second token after splitting with -
and converting the token to an integer:
sorted(codelist, key=lambda s: -int(s.split('-')[1]))
Upvotes: 1