dsdv76767671 2018-03-04 05:24
浏览 13

使用与值GO匹配的键对地图数组进行排序

I have just started programming in Golang and want to sort an array of maps. I have an array of maps. Let's call it example_array.

example_array = [
  [
    { Name: "A", Value: 100 }, 
    { Name: "B", Value: 60 }, 
    { Name: "C", Value: 170 }, 
    { Name: "D", Value: 120} 
  ], 
  [
    { Name: "A", Value: 64 }, 
    { Name: "B", Value: 90 }, 
    { Name: "C", Value: 52 }, 
    { Name: "D", Value: 98} 
  ], 
  [
    { Name: "A", Value: 154 }, 
    { Name: "B", Value: 190 }, 
    { Name: "C", Value: 179 }, 
    { Name: "D", Value: 67 } 
  ]

]

Now I want to sort this array using value of key "C" so the example_array should be modified to ->

[
[{Name: "A", Value: 64}, {Name: "B", Value: 90}, {Name: "C", Value: 52}, {Name: "D", Value: 98}],
[{Name: "A", Value: 100}, {Name: "B", Value: 60}, {Name: "C", Value: 170}, {Name: "D", Value: 120}], 
[{Name: "A", Value: 154}, {Name: "B", Value: 190}, {Name: "C", Value: 179}, {Name: "D", Value: 67}]
]

If I sort the original array using value of key "D", the original array should be modified to ->

[
[{Name: "A", Value: 154}, {Name: "B", Value: 190}, {Name: "C", Value: 179}, {Name: "D", Value: 67}],
[{Name: "A", Value: 64}, {Name: "B", Value: 90}, {Name: "C", Value: 52}, {Name: "D", Value: 98}]
[{Name: "A", Value: 100}, {Name: "B", Value: 60}, {Name: "C", Value: 170}, {Name: "D", Value: 120}]
]

How can I sort these array of maps in Golang. Please help!

  • 写回答

1条回答 默认 最新

  • doujiao1814 2018-03-04 06:32
    关注

    Your data looks like it could be easily represented as a slice of maps of type map[string]int. Since you didn't provide any Go code in your question, I cannot be sure of the data types, so I will assume it is a slice of maps of type map[string]int in this answer.

    A simple way to sort a slice of maps is to use the sort.Slice function. From a comment in the first example in the sort package documentation:

    use sort.Slice with a custom Less function, which can be provided as a closure. In this case no methods are needed

    The Less function needs to satisfy the signature

    func(i, j int) bool
    

    Per package documentation (at Interface):

    Less reports whether the element with index i should sort before the element with index j.

    Using a closure allows you to reference your data structure in the function body even though it is not part of the parameter list.

    Here's a runnable example that sorts a slice of map[string]int values matching the data in your question:

    package main
    
    import(
        "fmt"
        "sort"
    )
    
    func main() {
        in := []map[string]int{
            {
              "A": 100,
              "B": 60,
              "C": 170,
              "D": 120,
            },
            {
              "A": 64,
              "B": 90,
              "C": 52,
              "D": 98,
            },
            {
              "A": 154,
              "B": 190,
              "C": 179,
              "D": 67,
            },
        }
        for k, _ := range in[0] {
            sort.Slice(in, func(i, j int) bool { return in[i][k] < in[j][k] })
            fmt.Printf("By %s: %v
    ", k, in)
        }
    }
    

    Output:

    By A: [map[A:64 B:90 C:52 D:98] map[A:100 B:60 C:170 D:120] map[A:154 B:190 C:179 D:67]]
    By B: [map[A:100 B:60 C:170 D:120] map[B:90 C:52 D:98 A:64] map[C:179 D:67 A:154 B:190]]
    By C: [map[A:64 B:90 C:52 D:98] map[A:100 B:60 C:170 D:120] map[A:154 B:190 C:179 D:67]]
    By D: [map[A:154 B:190 C:179 D:67] map[B:90 C:52 D:98 A:64] map[A:100 B:60 C:170 D:120]]
    
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值