I have a list with dictionaries in which I sort them on different values. I'm doing it with these lines of code:
def orderBy(self, col, dir, objlist):
if dir == 'asc':
sorted_objects = sorted(objlist, key=lambda k: k[col])
else:
sorted_objects = sorted(objlist, key=lambda k: k[col], reverse=True)
return sorted_objects
Now the problem is that I occasionally have null values or empty strings when I try to sort and then it all breaks down.
I'm not sure but i think that this is the exception that's thrown: unorderable types: NoneType() < NoneType(). It occurs when there is None values on the column I'm trying to sort. For empty string values it works although they end up first in the list but I would like them to be last.
How can I solve this problem?