1 Star 0 Fork 0

Uni-Minds/utils

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
sort.go 1.25 KB
Copy Edit Raw Blame History
Liuxy authored 2022-10-15 14:14 . Release 1.0.0
/*
* Copyright (c) 2019-2020
* Author: LIU Xiangyu
* File: sort.go
*/
package tools
import (
"fmt"
"sort"
)
type mapSorter []MapItem
func MediaSorter(m map[int]interface{}) mapSorter {
ms := make(mapSorter, 0, len(m))
for key, value := range m {
ms = append(ms, MapItem{
Mid: key,
Value: value,
})
}
return ms
}
func RemoveDuplicateInt(a []int) []int {
sort.Ints(a)
i := 0
for j := 1; j < len(a); j++ {
if a[i] != a[j] {
i++
a[i] = a[j]
}
}
return a[:i+1]
}
func RemoveElementInt(a []int, ele int) []int {
a = RemoveDuplicateInt(a)
for k, v := range a {
if ele == v {
return append(a[:k], a[k+1:]...)
}
}
return a
}
type MapItem struct {
Mid int
Value interface{}
}
func (ms mapSorter) Len() int {
return len(ms)
}
func (ms mapSorter) Swap(i, j int) {
ms[i], ms[j] = ms[j], ms[i]
}
func (ms mapSorter) Less(i, j int) bool {
if ms[i].Value == ms[j].Value {
return ms[i].Mid < ms[j].Mid
} else {
switch ms[i].Value.(type) {
case string:
return ms[i].Value.(string) < ms[j].Value.(string)
case int:
return ms[i].Value.(int) < ms[j].Value.(int)
case float64:
return ms[i].Value.(float64) < ms[j].Value.(float64)
default:
fmt.Println("Unknow sort type", ms[i].Value)
return false
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/uni-minds/utils.git
git@gitee.com:uni-minds/utils.git
uni-minds
utils
utils
v1.0.2

Search