简体   繁体   中英

How to sort a table built by HTML.py

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM