2

I have a list of strings like below.

>>> mylist = ["2013-09-07T11:23:43", "2013-09-05T15:14:12", "2013-10-13T22:06:22", "2013-11-16T23:01:34", "2013-12-22T23:11:54"]

I want to sort these as they are dates.

>>> mylist.sort()

This seems to work as I would like but will this always work with any date in this format?

>>> mylist
['2013-09-05T15:14:12', '2013-09-07T11:23:43', '2013-10-13T22:06:22', '2013-11-16T23:01:34', '2013-12-22T23:11:54']
0

1 Answer 1

8

Yes, that works with any date in that format, because they are:

  1. using the same length, zero-padded numbers
  2. in the same order
  3. ordered by most significant to least significant; year, month, day, hour, minute, second

This makes them lexicographically orderable, which is exactly the order sorting would apply.

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

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.