Python: sorting list of lists not functioning as intended
Question: I am trying to sort the following list of lists by the first item of each list
in ascending order:

    
    
    framenos = [
        ['1468', '2877', 'Pos.:', 95],
        ['3185', '4339', 'Pos.:', 96],
        ['195', '1460', 'Pos.:', 97]
    ]
    

I am using the following to do so:

    
    
    framesorted = sorted(framenos, key=lambda x: x[0]) #sort ranges by start numbers
    

Which gives:

    
    
    [['1468', '2877', 'Pos.:', 95], ['195', '1460', 'Pos.:', 97], ['3185', '4339', 'Pos.:', 96]]
    

What's going wrong?


Answer: Your values are strings, so you are sorting
[_lexicographically_](https://en.wikipedia.org/wiki/Lexicographical_order),
not numerically. `'1468'` is sorted before `'195'` because `'4'` comes before
`'9'` in the ASCII standard, just like `'Ask'` would be sorted before
`'Attribution'`.

Convert your strings to numbers if you need a numeric sort:

    
    
    framesorted = sorted(framenos, key=lambda x: int(x[0]))
    

Demo:

    
    
    >>> framenos = [
    ...     ['1468', '2877', 'Pos.:', 95],
    ...     ['3185', '4339', 'Pos.:', 96],
    ...     ['195', '1460', 'Pos.:', 97]
    ... ]
    >>> sorted(framenos, key=lambda x: int(x[0]))
    [['195', '1460', 'Pos.:', 97], ['1468', '2877', 'Pos.:', 95], ['3185', '4339', 'Pos.:', 96]]
    >>> from pprint import pprint
    >>> pprint(_)
    [['195', '1460', 'Pos.:', 97],
     ['1468', '2877', 'Pos.:', 95],
     ['3185', '4339', 'Pos.:', 96]]
    

