I have a fairly simple list (a number followed by a sentence), here in the right order:
-347 a negative number
-100 another negative number
-25 and again, a negative number
17 some text
25 foo bar
100 two same texts
100 two same texts (almost)
350 a positive number
I need to sort that list each time a new item is added.
I searched S.O. and found that answer :
Sorting in python - how to sort a list containing alphanumeric values?
The code I used is freakish’s, which goes :
import re
def convert(str):
return int("".join(re.findall("\d*", str)))
list1.sort(key=convert)
In order to better explain my problem, I shuffled the list and ran the code.
The result was :
17 some text
-25 and again, a negative number
25 foo bar
100 two same texts (almost)
100 two same texts
-100 another negative number
-347 a negative number
350 a positive number
What went wrong ? What is the precision in the code that would sort naturally the negative numbers?