Dong.Ge

左手代码 右手江湖

golang map sort

东哥's Avatar 2019-05-22

golang的map是无序的,通过下面3种曲线救国的方式,可以实现有序.
第一种,是用sort排序.

第二种,是利用结构体和切片.

第三种,比较复杂,用golang自带的list数据结构实现.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main

import (
"container/list"
"fmt"
"sort"
)

type mySort []map[string]interface{}

func (a mySort) Less(i, j int) bool {
if a[i]["weight"].(int) > a[j]["weight"].(int) {
return true
} else if a[i]["weight"].(int) < a[j]["weight"].(int) {
return false
} else if a[i]["weight"].(int) == a[j]["weight"].(int) {
if a[i]["id"].(int) > a[j]["id"].(int) {
return true
}
}
return false
}

func (a mySort) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
func (a mySort) Len() int {
return len(a)
}

func one() {
var wcd = mySort{
map[string]interface{}{
"name": "name1",
"id": 123,
"weight": 1,
},
map[string]interface{}{
"name": "name2",
"id": 234,
"weight": 3,
},
map[string]interface{}{
"name": "name3",
"id": 5445,
"weight": 2,
},
map[string]interface{}{
"name": "name4",
"id": 121212,
"weight": 2,
},
}
//优先 按照weight desc 排序
//当weight相等 按照id desc 排序
sort.Sort(wcd)
fmt.Println(wcd)
}

func two() {
bothOk := []string{
"hello",
"qq",
}
bothInvild := []string{
"ni",
"hao",
}
oNoSort := map[string][]string{
"bothOk": bothOk,
"bothInvild": bothInvild,
}
for k, _ := range oNoSort {
fmt.Println(k)
}
type one struct {
Key string
Ids []string
}
oSort := []one{
{
Key: "bothOk",
Ids: bothOk,
},
{
Key: "bothInvild",
Ids: bothInvild,
},
}

for _, ll := range oSort {
fmt.Println(ll.Key)
}
}

type Keyer interface {
GetKey() string
}

type MapList struct {
dataMap map[string]*list.Element
dataList *list.List
}

func NewMapList() *MapList {
return &MapList{
dataMap: make(map[string]*list.Element),
dataList: list.New(),
}
}

func (mapList *MapList) Exists(data Keyer) bool {
_, exists := mapList.dataMap[data.GetKey()]
return exists
}

func (mapList *MapList) Push(data Keyer) bool {
if mapList.Exists(data) {
return false
}
elem := mapList.dataList.PushBack(data)
mapList.dataMap[data.GetKey()] = elem
return true
}

func (mapList *MapList) Remove(data Keyer) {
if !mapList.Exists(data) {
return
}
mapList.dataList.Remove(mapList.dataMap[data.GetKey()])
delete(mapList.dataMap, data.GetKey())
}

func (mapList *MapList) Size() int {
return mapList.dataList.Len()
}

func (mapList *MapList) Walk(cb func(data Keyer)) {
for elem := mapList.dataList.Front(); elem != nil; elem = elem.Next() {
cb(elem.Value.(Keyer))
}
}

type Elements struct {
key string
ids []string
}

func (e Elements) GetKey() string {
return e.key
}

func main() {
ml := NewMapList()
bothOk := []string{"hello", "golang"}
bothInvild := []string{"fuck", "bad"}
a := &Elements{"bothOk", bothOk}
b := &Elements{"bothInvild", bothInvild}
ml.Push(a)
ml.Push(b)

for elem := ml.dataList.Front(); elem != nil; elem = elem.Next() {
key := elem.Value.(Keyer).GetKey()
fmt.Println("list:", key, elem.Value.(*Elements).ids)
fmt.Println("map:", ml.dataMap[key].Value.(*Elements).key, ml.dataMap[key].Value.(*Elements).ids)
}
}

本文最后更新于 天前,文中所描述的信息可能已发生改变