Reputation: 8816
I have table built using HTML.py utility which has a column "Date". Is there any way I can sort the table by date? The rows in table are appended to table by a list of python dictionaries which has a key date in each dictionary element.
Upvotes: 0
Views: 66
Reputation: 257
Yes using sorted method you can achieve that. but say for example, if you have saved entire table in one dictionary and which is an element of list. then in that case the above code may not work. : example: l = [{3:'test', 2:'kranthi', 1:'dfdf'}]. , so you can achieve in both the cases using below piece of code
def _sortListOfDict(_listOfDict):
return sorted(_listOfDict, key=lambda x: x.items()[0])
The above simple program will sort the entire list of dictionaries and return the sorted list of dictionary
Upvotes: 1
Reputation: 8816
Since it was list of dictionaries I created a sorted list using
newlist = sorted(list_to_be_sorted, key=lambda k: k['Date'])
and then built the table.
Upvotes: 0