0

I have a list of "snapshots" which look like this

{ description: "sdoiajosdi", "startDate": datetime.datetime(2021, 7, 16, 22, 47, 50, 609000, tzinfo=tzlocal()), Tags: [ {'sdklapsijd':'asdjiosoid'}], ownerId: "osjdaiosjd" }

These are stored in a python list. Is there a way I can sort ascending or descending based on the 'startDate' property for each item in the list? So if I have 100 snapshot objects, with differing datetime.datetime properties, can I use this as the 'sorting key?' How can I do this?

2

1 Answer 1

1

As datetime objects are comparable, you should just be able to sort the list with:

sorted(snapshots, key=lambda x: x["startDate"])
Sign up to request clarification or add additional context in comments.

1 Comment

That looks like what I was just reading! Thanks :)

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.