golangMap进行键值自定义排序

xiaoxiao2021-02-28  21

//自定义结构体,用来自定义排序 type RecentlyUpdateListMapSorter []map[string]interface{} //根据MapSorter.Keys进行倒序排序 func (ms *RecentlyUpdateListMapSorter) SortReverse() { sort.Sort(sort.Reverse(ms)) } //自定义排序方法(Len(),Less(),Swap()) func (ms *RecentlyUpdateListMapSorter) Len() int { return len(*ms) } func (ms *RecentlyUpdateListMapSorter) Less(i, j int) bool { t1, _ := time.Parse(timex.Layout_Normal, core.ToString((*ms)[i]["UpdateTime"])) t2, _ := time.Parse(timex.Layout_Normal, core.ToString((*ms)[j]["UpdateTime"])) return t1.Before(t2) } func (ms *RecentlyUpdateListMapSorter) Swap(i, j int) { (*ms)[i], (*ms)[j] = (*ms)[j], (*ms)[i] }
转载请注明原文地址: https://www.6miu.com/read-1650247.html

最新回复(0)