golang怎么对日期和时间进行排序?
时间:2023-01-08
浏览:336
作者:佚名
亲爱的编程学习爱好者,如果你点开了这篇文章,说明你对《golang怎么对日期和时间进行排序?》很感兴趣。本篇文章就来给大家详细解析一下,主要介绍一下time、go、sort,希望所有认真读完的童鞋们,都有实质性的提高。
问题内容
如下一个文本文件,需要以时间的先后顺序进行排序。
20161206 2016-12-06 13:34:31 +0800 shut off snapshot1 2016-06-20 15:57:35 +0800 shut off snapshot2 2016-03-21 15:57:25 +0800 shut off auto_FF_61261206_180504 2014-10-06 18:05:04 +0800 running auto_FF_61261206_231115 2016-12-06 13:11:15 +0800 shut off auto_FF_61261206_235349 2016-12-06 12:53:49 +0800 running
正确答案
先自己按行解析出来,解析成map[string]string
要是想解析成[]map[string]string 就把下面代码改改就行
import "sort"
type MapSorter []SortItem
type SortItem struct {
Key string `json:"key"`
Val interface{} `json:"val"`
}
func (ms MapSorter) Len() int {
return len(ms)
}
func (ms MapSorter) Less(i, j int) bool {
return ms[i].Key < ms[j].Key // 按键排序
// return ms[i].Value 好了,本文到此结束,带大家了解了《golang怎么对日期和时间进行排序?》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
11.28
11.28
11.28
11.28
热门专题
