簡體   English   中英

如何在 go lang 中對 map[string]interface{} 類型進行多重排序?

[英]How to multiple sort a map[string]interface{} type in go lang?

場景:假設我有一個 JSON 數據要在 go lang 中處理現在我正在使用map[string]interface{}類型使用 package encoding/json通過執行marshal/unmarshal

下面是JSON的數據:

{
    "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
    }
}

現在我想根據優先級執行多排序降序,比如優先級是TemperaturePopulationVehicleCount那么我希望 output 像

{
    "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
    }
}

因此,根據一些動態的優先級進行排序。

問題:我不知道如何在 go lang 中對其進行排序。 我是 go lang 的新手,在搜索對我的數據進行排序時發現了一些東西; 如下所述(來源:鏈接

使用范圍循環迭代 map 時,未指定迭代順序,並且不保證從一次迭代到下一次迭代是相同的。

問題:誰能闡明如何對類似的 JSON 數據進行這種排序?

這個答案更多地是關於解決此問題的方法,而不是特定的解決方案。 我認為這是您目前更需要的,因為您還沒有接近實際的解決方案。 這是簡單的程序步驟;

1)為此定義一個類型,我從這里開始將其稱為City

"TumkurCity": {
    "Population": 800,
    "VehicleCount": 300,
    "Temperature": 29
}

type City struct {
    Population   int
    VehicleCount int
    Temperature  int
    Name         string
}

2)解組map[string]City

3)使用類似的方法來轉換數據;

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)定義一些方法來對城市進行排序

我在瀏覽器中編寫了該代碼,因此未經過任何測試。 它應該提供如何處理它的充分示例。 取消編組時,您的城市類型將沒有名稱值,因此您以后可以為它分配密鑰(城市名稱)。 將它們放在切片或陣列中,然后進行排序。 如果遵循這些步驟,則非常簡單。

我執行了此功能,從N個圖及其排序鍵中獲取了一個結果圖。

func getSortedKeys(maps ...map[string]string) (map[string]string, []string) {
    var keys []string
    resultMap := map[string]string{}

    for _, currMap := range maps {
        for k, v := range currMap {
            resultMap[k] = v
            keys = append(keys, k)
        }
    }
    sort.Strings(keys)
    return resultMap, keys

}

在功能之外,您可以執行以下操作:

  concatenedMaps, keys := getSortedKeys(map1,map2)
  for _, k := range keys {
        value := concatenedMaps[k]
  }

你可以使用https://github.com/rxwycdh/rxhash rxhash.SortMap ,可以按鍵對嵌套復雜的 map 進行排序。

例如:

import "github.com/rxwycdh/rxhash"

dd := map[string]any{
    "MysoreCity": map[string]any{
        "Population":   1000,
        "VehicleCount": 1700,
        "Temperature":  33,
    },
    "BangaloreCity": map[string]any{
        "Population":   1000,
        "VehicleCount": 3500,
        "Temperature":  33,
    },
    "KolarCity": map[string]any{
        "Population":   1250,
        "VehicleCount": 3500,
        "Temperature":  31,
    },
    "TumkurCity": map[string]any{
        "Population":   800,
        "VehicleCount": 300,
        "Temperature":  29,
    },
}

b, _ := json.Marshal(rxhash.SortMap(dd))
println(string(b))

output:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM