0

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.

2
  • Python dicts are unordered. You might be able to convert it to an ordereddict and sort by date. Commented Apr 29, 2016 at 5:20
  • decalage.info/en/python/html Commented Apr 29, 2016 at 5:44

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.