python - 老铁们,在列表中如何对时间进行排序~
巴扎黑
巴扎黑 2017-04-18 10:34:39
0
3
455
fx  = [{'次数': 2, '时间': '10:45:00'}, {'次数': 4, '时间': '14:58:00'}, {'次数': 5, '时间': '09:59:00'}, {'次数': 28, '时间': '08:13:00'}, {'次数': 34, '时间': '08:28:00'}, {'次数': 4, '时间': '05:12:00'}, {'次数': 12, '时间': '04:56:00'}]

上面的这个列表中,进行时间上的排列,比如最后那个是4点56的,排在第一位,9点59的那个排在最后面。

老铁们,帮帮忙啊~~谢谢

巴扎黑
巴扎黑

reply all(3)
伊谢尔伦
fx.sort(key=lambda item: int(item['时间'][:2])*60 + int(item['时间'][3:5]))
伊谢尔伦

Take out the time, desc

黄舟
    fx.sort(function (pre, next) {
        return new Date('1970/01/01 ' + pre['时间']) - new Date('1970/01/01 ' + next['时间'])
    });
    console.log(JSON.stringify(fx))
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!