sorting - 对 map slice 进行排序

标签 sorting dictionary go slice

我有一个包含 10 个 map 的 slice ,如何按其中一个值排序?

[{"name":"a","score":100},
{"name":"z","score":150},
{"name":"s","score":120},
{"name":"x","score":110},
{"name":"r","score":10},
{"name":"b","score":1300},
{"name":"b","score":1500},
{"name":"w","score":1002},
{"name":"o","score":140},
{"name":"q","score":101}]

我想用 score 值对 slice 进行降序排序,但我不知道如何排序。

最佳答案

只是一个简单的 sort.Slice

sort.Slice(information, 
func(i, j int) bool { 
    return information[i].Difference > information[j].Difference 
})

关于sorting - 对 map slice 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48291202/

相关文章:

PHP 基于多个条件对多维数组进行排序

python 3 : matplotlib plotting four lines with dictionary

c++ - map 的查找功能得到错误的结果

c# - 创建任何对象的字典

postgresql - 如何在 Postgresql 时间戳中存储 Golang time.time?

dictionary - 与Go中的 map 相比,搜索无序数组要花多少个元素

c++ - 这是什么排序算法?

performance - 我们如何通过串联数组中的整数有效地找到最小整数?

postgresql - Docker-compose Go 应用程序和 Postgres 之间的通信问题

python - 按 MultiIndex 的一级对 pandas DataFrame 进行排序