排序地图切片

时间:2018-01-16 23:01:17

标签: sorting dictionary go slice

我有一张10张地图,我怎样才能按其中一个值排序?

[{"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值按降序对切片进行排序,但我不知道如何。

1 个答案:

答案 0 :(得分:2)

只是一个简单的sort.Slice

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