2024年4月24日发(作者:宿阳荣)
golangmap数组根据某个字段值排序
package main
import (
"fmt"
"math/rand"
"sort"
"strconv"
"zentaotimer/libs//wonderivan/logger"
)
type MapsSort struct {
Key string
MapList []map[string] interface{}
}
func (m *MapsSort) Len() int {
return len(t)
}
func (m *MapsSort) Less(i, j int) bool {
var ivalue float64
var jvalue float64
var err error
n()
switch t[i][].(type) {
case string:
ivalue,err = loat(t[i][].(string),64)
if err != nil {
("map数组排序string转float失败:%v",err)
return true
}
case int:
ivalue = float64(t[i][].(int))
case float64:
ivalue = t[i][].(float64)
case int64:
ivalue = float64(t[i][].(int64))
}
switch t[j][].(type) {
case string:
jvalue,err = loat(t[j][].(string),64)
if err != nil {
("map数组排序string转float失败:%v",err)
return true
}
case int:
jvalue = float64(t[j][].(int))
case float64:
jvalue = t[j][].(float64)
case int64:
jvalue = float64(t[j][].(int64))
}
return ivalue > jvalue
}
func (m *MapsSort) Swap(i, j int) {
t[i],t[j] = t[j],t[i]
}
func main() {
mapsSort := MapsSort{}
= "data"
maps:= make([]map[string] interface{},0)
for i:=0 ; i<10;i++ {
data := 64()
mapTemp := make(map[string] interface{})
mapTemp["data"] = data
mapTemp["aaa"] = f("aaa%d",i)
maps = append(maps, mapTemp)
}
mapTemp := make(map[string] interface{})
mapTemp["data"] = "1.001"
mapTemp["aaa"] = f("aaa")
maps = append(maps, mapTemp)
n(maps)
t = maps
n(mapsSort)
(&mapsSort)
n(mapsSort)
}
2024年4月24日发(作者:宿阳荣)
golangmap数组根据某个字段值排序
package main
import (
"fmt"
"math/rand"
"sort"
"strconv"
"zentaotimer/libs//wonderivan/logger"
)
type MapsSort struct {
Key string
MapList []map[string] interface{}
}
func (m *MapsSort) Len() int {
return len(t)
}
func (m *MapsSort) Less(i, j int) bool {
var ivalue float64
var jvalue float64
var err error
n()
switch t[i][].(type) {
case string:
ivalue,err = loat(t[i][].(string),64)
if err != nil {
("map数组排序string转float失败:%v",err)
return true
}
case int:
ivalue = float64(t[i][].(int))
case float64:
ivalue = t[i][].(float64)
case int64:
ivalue = float64(t[i][].(int64))
}
switch t[j][].(type) {
case string:
jvalue,err = loat(t[j][].(string),64)
if err != nil {
("map数组排序string转float失败:%v",err)
return true
}
case int:
jvalue = float64(t[j][].(int))
case float64:
jvalue = t[j][].(float64)
case int64:
jvalue = float64(t[j][].(int64))
}
return ivalue > jvalue
}
func (m *MapsSort) Swap(i, j int) {
t[i],t[j] = t[j],t[i]
}
func main() {
mapsSort := MapsSort{}
= "data"
maps:= make([]map[string] interface{},0)
for i:=0 ; i<10;i++ {
data := 64()
mapTemp := make(map[string] interface{})
mapTemp["data"] = data
mapTemp["aaa"] = f("aaa%d",i)
maps = append(maps, mapTemp)
}
mapTemp := make(map[string] interface{})
mapTemp["data"] = "1.001"
mapTemp["aaa"] = f("aaa")
maps = append(maps, mapTemp)
n(maps)
t = maps
n(mapsSort)
(&mapsSort)
n(mapsSort)
}