In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "how to sort Map according to Value size in Golang". The editor shows you the operation process through an actual case. The method of operation is simple, fast and practical. I hope this article "how to sort Map in Golang according to Value size" can help you solve the problem.
cause
Recently, there is a requirement in the project:
Get the preset city name near the user according to the user's current coordinate point.
One caveat here is to assume that these supported city names are preset, so you can't get the city name directly from the coordinate points through the map class api.
The solution that comes to mind is:
Get the coordinate points of these preset cities
The App side acquires the user's current coordinate points
The distance between the user coordinate point and each preset city is calculated respectively.
Then the term with the smallest distance is calculated.
The city corresponding to this coordinate point is what is required.
Explore
After previous calculations, I got the following results after step 3 above:
Result: = map [string] float64 {
City A: 2334.20
City B: 1992.33
City C: 500.26
"City D": 10.39
"City E": 333.33
}
We know that Map in Golang is unordered. So when we use the for-range loop:
For k, v: = range result {fmt.Printf ("key:% v value:% v\ n", k, v)}
The result may be:
/ / the first possible outcome:
Key: city B value: 1992.33
Key: city C value: 500.26
Key: city D value: 10.39
Key: city E value: 333.33
Key: city A value: 2334.2
/ / the second possible outcome:
Key: city E value: 333.33
Key: city A value: 2334.2
Key: city B value: 1992.33
Key: city C value: 500.26
Key: city D value: 10.39
/ / the third possible outcome:
Key: city E value: 333.33
Key: city A value: 2334.2
Key: city B value: 1992.33
Key: city C value: 500.26
Key: city D value: 10.39
Therefore, we cannot sort by key or value.
Realize
But the slice Slice in Golang is ordered. We can sort the Map using Slice as a result.
The first step
Let's first convert the above map into a slice:
Type KVPair struct {Key string Val float64} tmpList: = make ([] KVPair, 0) for k, v: = range result {tmpList = append (tmpList, KVPair {Key: K, Val: v})}
A structure slice is created above, and then the value of map is added to the slice.
Step two
After go1.8, the sort.Slice () method is introduced to sort the slice. We only need to pass in a comparison function:
Sort.Slice (tmpList, func (I, j int) bool {return tmpList [I] .Val
< tmpList[j].Val // 升序 })第三步 然后,我们对排序后的 slice 进行 for-range 遍历: for _, pair := range tmpList { fmt.Printf("key: %v value: %v \n", pair.Key, pair.Val) }// 结果:key: 城市D value: 10.39 key: 城市E value: 333.33 key: 城市C value: 500.26 key: 城市B value: 1992.33 key: 城市A value: 2334.2 可以看到,排序后的 slice 第一项就是我们想要的结果。 如果我们想要获取其中 value 值最大的一项,只需要更改 sort.Slice 中的比较方法接口: sort.Slice(tmpList, func(i, j int) bool { return tmpList[i].Val >TmpList.Val / / descending / / return tmpList.Val
< tmpList[j].Val // 升序})总结 上面测试的完整代码如下: package main import ( "fmt" "sort") var result = map[string]float64{ "城市A": 2334.20, "城市B": 1992.33, "城市C": 500.26, "城市D": 10.39, "城市E": 333.33, }func main() { type KVPair struct { Key string Val float64 } tmpList := make([]KVPair, 0) for k, v := range result { tmpList = append(tmpList, KVPair{Key: k, Val: v}) } sort.Slice(tmpList, func(i, j int) bool { //return tmpList[i].Val >TmpList [j] .Val / / descending return tmpList [I] .Val < tmpList [j] .Val / / ascending order}) for _, pair: = range tmpList {fmt.Printf ("key:% v value:% v\ n", pair.Key, pair.Val)}} about "how Map is sorted by Value size in Golang" is here. Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.