How do I sort a list of datetime or date objects?

后端 未结 1 673
谎友^
谎友^ 2020-12-08 17:55

How do I sort a list of date and/or datetime objects? The accepted answer here isn\'t working for me:

from datetime import datetime,date,timedelta


a=[date.         


        
相关标签:
1条回答
  • 2020-12-08 18:38

    You're getting None because list.sort() it operates in-place, meaning that it doesn't return anything, but modifies the list itself. You only need to call a.sort() without assigning it to a again.

    There is a built in function sorted(), which returns a sorted version of the list - a = sorted(a) will do what you want as well.

    0 讨论(0)
提交回复
热议问题