Fetch the repository succeeded.
/*
* 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
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。