golang Map进行键值自定义排序

时间:2025-03-03 16:10:12
//自定义结构体,用来自定义排序
type RecentlyUpdateListMapSorter []map[string]interface{}

//根据进行倒序排序
func (ms *RecentlyUpdateListMapSorter) SortReverse() {
	((ms))
}

//自定义排序方法(Len(),Less(),Swap())
func (ms *RecentlyUpdateListMapSorter) Len() int {
	return len(*ms)
}
func (ms *RecentlyUpdateListMapSorter) Less(i, j int) bool {
	t1, _ := (timex.Layout_Normal, ((*ms)[i]["UpdateTime"]))
	t2, _ := (timex.Layout_Normal, ((*ms)[j]["UpdateTime"]))
	return (t2)
}
func (ms *RecentlyUpdateListMapSorter) Swap(i, j int) {
	(*ms)[i], (*ms)[j] = (*ms)[j], (*ms)[i]
}

相关文章