P Pariventhan
P Pariventhan

Reputation: 43

sort numbers with corresponding string in descending order

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

Answers (1)

blhsing
blhsing

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

Related Questions