I am attempting to sort a list of lists by each index of the inner list. (All inner lists are the same length.) The goal is to sort the rows by the last column (the last index of the inner lists) first, then by the previous column/index and so on.
Input:
[
['2016', 'E', None, '68', '94'],
['2016', 'A', None, '91', '25'],
['2016', 'C', None, '74', '25'],
['2017', 'C', None, '55', '20'],
['2015', 'D', None, '20', '14'],
['2016', 'B', None, '66', '66'],
['2017', 'E', None, '29', '41'],
['2017', 'F', None, '61', '22'],
['2015', 'A', None, '17', '96']
]
Output:
[
['2015', 'A', None, '17', '96'],
['2015', 'D', None, '20', '14'],
['2016', 'A', None, '91', '25'],
['2016', 'B', None, '66', '66'],
['2016', 'C', None, '74', '25'],
['2016', 'E', None, '68', '94'],
['2017', 'C', None, '55', '20'],
['2017', 'E', None, '29', '41'],
['2017', 'F', None, '61', '22']
]
I have the following piece of code that I'm trying to use for this:
def sort_table(column_count, rows)
for i in range(len(column_count) - 1, -1, -1):
rows = sorted(rows, key=operator.itemgetter(i))
return rows
However, it seems to be thrown by the fact that there are or can be None
values in the list. I keep getting the error TypeError: '<' not supported between instances of 'NoneType' and 'str'
. Is there a correct way to handle this?