I have the following list, which was created from a file:
scores=["bob.21","jeff.46","michael.75","david.12"] 
How can I sort items of this list based on the integer within each item?
            Asked
            
        
        
            Active
            
        
            Viewed 59 times
        
    0
            
            
        - 
                    2(Dupe of) [How to sort with lambda in Python](https://stackoverflow.com/questions/3766633/how-to-sort-with-lambda-in-python) + some string parsing – hnefatl Aug 31 '20 at 13:37
- 
                    It does, thank you so much! – Awesome Guy Aug 31 '20 at 13:39
2 Answers
1
            
            
        Use a key function that parses out the integer:
scores.sort(key=lambda x: int(x.partition('.')[2]))
That just logically sorts it as if the values were [21, 46, 75, 12] while preserving the original values.
 
    
    
        ShadowRanger
        
- 143,180
- 12
- 188
- 271
- 
                    1@timgeb: Not when they're all two digits long (though it's harmless to convert, and might make the comparisons infinitesimally faster). But it's absolutely necessary if there might be a different number of digits. `20 < 3` is false, but `'20' < '3'` is true. – ShadowRanger Aug 31 '20 at 13:40
0
            
            
        scores = ["bob.21","jeff.46","michael.75","david.12"]
sorted_scores = sorted(scores,key=lambda s: int(s.split('.')[1]))
print(sorted_scores)
output
['david.12', 'bob.21', 'jeff.46', 'michael.75']
 
    
    
        balderman
        
- 22,927
- 7
- 34
- 52
