可以使用Python中的datetime模块将字符串转换为日期,并使用sorted函数对数组进行排序。
示例代码如下:
from datetime import datetime
# 定义一个字符串数组
date_strings = ['2022-01-03', '2021-11-29', '2021-12-28', '2021-12-02']
# 使用lambda函数将字符串转换为日期
date_objects = list(map(lambda x: datetime.strptime(x, '%Y-%m-%d'), date_strings))
# 使用sorted函数按照日期进行排序
sorted_dates = sorted(date_objects)
# 输出排序结果
for date in sorted_dates:
print(date.strftime('%Y-%m-%d'))
输出结果为:
2021-11-29
2021-12-02
2021-12-28
2022-01-03