dpxnrx11199 2015-04-09 10:44
浏览 551
已采纳

如何对go lang中的map [string] interface {}类型进行多重排序?

Scenario: Consider I have a JSON data to be processed in go lang Now I am using map[string]interface{} type using package encoding/json by doing marshal/unmarshal

Below is the JSON data:

{
    "MysoreCity": {
        "Population": 1000,
        "VehicleCount": 1700,
        "Temperature": 33
    },
    "BangaloreCity": {
        "Population": 1000,
        "VehicleCount": 3500,
        "Temperature": 33
    },
    "KolarCity": {
        "Population": 1250,
        "VehicleCount": 3500,
        "Temperature": 31
    },
    "TumkurCity": {
        "Population": 800,
        "VehicleCount": 300,
        "Temperature": 29
    }
}

Now I want to perform a multi-sort descending order on the basis of priority, say priority is Temperature, Population, VehicleCount then I want the output to be like

{
    "BangaloreCity": {
        "Population": 1000,
        "VehicleCount": 3500,
        "Temperature": 33
    },
    "MysoreCity": {
        "Population": 1000,
        "VehicleCount": 1700,
        "Temperature": 33
    },
    "KolarCity": {
        "Population": 1250,
        "VehicleCount": 3500,
        "Temperature": 31
    },
    "TumkurCity": {
        "Population": 800,
        "VehicleCount": 300,
        "Temperature": 29
    }
}

So the sorting with respect to some dynamic set of priorities.

Issue: I am not getting any clues how to sort it in go lang. I am new to go lang and found something while searching for sorting my data; where it is mentioned as below (source:link)

When iterating over a map with a range loop, the iteration order is not specified and is not guaranteed to be the same from one iteration to the next.

Question: Could anyone put some light on how to do this kind of sorting with similar JSON data?

  • 写回答

2条回答 默认 最新

  • duanfu6160 2015-04-16 16:22
    关注

    This answer is more about the methodology for solving this problem than a specific solution. I think it's more what you need at this point because you're not close to an actual solution. Here are the simple procedural steps;

    1) define a type for this, I'll call it City from here on;

    "TumkurCity": {
        "Population": 800,
        "VehicleCount": 300,
        "Temperature": 29
    }
    
    type City struct {
        Population   int
        VehicleCount int
        Temperature  int
        Name         string
    }
    

    2) unmarshal into a map[string]City

    3) use a method like this to transform the data;

    func ToSlice(m map[string]City) []City {
        cities := make([]City, 0, len(m))
        for k, v := range m {
            v.Name = k
            cities = append(cities, v)
        }
        return cities
    }
    

    4) define some method to sort the cities however it is you want

    I wrote that code in the browser so it hasn't been tested or anything. It should provide a sufficient example of how to handle it. When you unmarshal your City types will have no value for name so you can just assign the key (the cities name) to that later on. put them in a slice or array, sort that. Pretty simple if you follow those steps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大