My list is like this:
10.987|first sentence
13.87|second sentence
9.098|third sentence
if I do something like:
for x in my_
Here is one way.
lst = ['10.987|first sentence',
'13.87|second sentence',
'9.098|third sentence']
res = sorted(lst, key=lambda x: -float(x.split('|')[0]))
Result
['13.87|second sentence',
'10.987|first sentence',
'9.098|third sentence']
Explanation
sorted
takes an argument key
which allows you to specify a custom (lambda
) function on which to sort.lambda
function splits by "|" and extracts the first part to get the numeric component.float
and finally negate to ensure descending order.reverse=True
argument may be used.