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?