排序datetime.datetime 2d数组

时间:2018-08-22 15:55:05

标签: python datetime

我有以下2D数组,日期格式为%Y-%m-%d%H:%M:%S:

[['second comment test', datetime.datetime(2018, 8, 22, 15, 49, 40), 's']
['this comment originated in service now', datetime.datetime(2018, 8, 22, 14, 45), 's']
['this is a longer description used for testing', datetime.datetime(2018, 8, 22, 14, 13, 49), 'z']]

我注意到,如果其中一个时间日志恰好是一分钟,例如 2018-08-22 14:45:00 ,这意味着它以00结尾,则不会记录秒。那么如何按日期排序此数组?

1 个答案:

答案 0 :(得分:1)

如@DietrichEpp所述,假设您拥有的是一个列表,则可以简单地通过以下方式对该数组进行排序:

list_ = [
    ['second comment test', datetime.datetime(2018, 8, 22, 15, 49, 40), 's'],
    ['this comment originated in service now', datetime.datetime(2018, 8, 22, 14, 45), 's'],
    ['this is a longer description used for testing', datetime.datetime(2018, 8, 22, 14, 13, 49), 'z'],]
list_.sort(key=lambda x: x[1])