Golang : How to reverse elements order in map ?



Tags : golang reverse-order element map

There is no built in function in Golang that allow you to reverse the elements order in a map, but it can be done easily with for loops. However, care must be taken to ensure that the reversed elements are stable. At this point of writing, the only way to ensure the reversed elements order is stable is to put them in a slice.

The codes example below will demonstrate how to reverse the elements order ... first in a unstable way and followed by the stable way.

First :

 package main

 import "fmt"

 func main() {

 cities := map[string]string{"city1": "New York", "city2": "Portland", "city3": "Seattle"}

 fmt.Println(cities)

 reversed_cities := make(map[string]string) // empty map

 for key, value := range cities {
 reversed_cities[value] = key
 }

 fmt.Println(reversed_cities)
 }

Output :

> go run reversemapwrong.go

map[city1:New York city2:Portland city3:Seattle]

map[New York:city1 Portland:city2 Seattle:city3]

> go run reversemapwrong.go

map[city1:New York city2:Portland city3:Seattle]

map[New York:city1 Portland:city2 Seattle:city3]

> go run reversemapwrong.go

map[city2:Portland city3:Seattle city1:New York]

map[Seattle:city3 New York:city1 Portland:city2]

After couple of executions, you will notice that the reversed order is not stable and this was explained here at http://blog.golang.org/go-maps-in-action#TOC_7.

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. Since Go 1 the runtime randomizes map iteration order, as programmers relied on the stable iteration order of the previous implementation. If you require a stable iteration order you must maintain a separate data structure that specifies that order.

Second :

 package main

 import (
 "fmt"
 "sort"
 )

 type reversedCities struct {
 Index int
 Name  string
 }

 func main() {

 cities := map[int]string{1: "New York", 2: "Portland", 3: "Seattle", 4 : "Boston"}

 fmt.Println(cities)

 var keys []int
 for k := range cities {
 keys = append(keys, k)
 }

 sort.Sort(sort.Reverse(sort.IntSlice(keys)))

 // sanity check
 fmt.Println(keys) // reversed key order

 var onerC reversedCities
 var rC []reversedCities

 for _, key := range keys {
 onerC.Index = key
 onerC.Name = cities[key]
 rC = append(rC, onerC)
 }

 reversed_cities := make(map[int]string) // empty map

 // put the reverse ordered elements into the new map

 for _, record := range rC {
 reversed_cities[record.Index] = record.Name
 }

 // this IS STILL NOT stable as well because of for loop range insertion as documented
 // at http://blog.golang.org/go-maps-in-action#TOC_7

 fmt.Println(reversed_cities)

 // this is stable and in slice form
 fmt.Println(rC)

 }

Output :

> go run reversemap.go

map[3:Seattle 4:Boston 1:New York 2:Portland]

[4 3 2 1]

map[4:Boston 3:Seattle 2:Portland 1:New York]

[{4 Boston} {3 Seattle} {2 Portland} {1 New York}]


>go run reversemap.go

map[1:New York 2:Portland 3:Seattle 4:Boston]

[4 3 2 1]

map[4:Boston 3:Seattle 2:Portland 1:New York]

[{4 Boston} {3 Seattle} {2 Portland} {1 New York}]


> go run reversemap.go

map[1:New York 2:Portland 3:Seattle 4:Boston]

[4 3 2 1]

map[1:New York 4:Boston 3:Seattle 2:Portland]

[{4 Boston} {3 Seattle} {2 Portland} {1 New York}]

  See also : Golang : Check if element exist in map



Tags : golang reverse-order element map

By Adam Ng

IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.


Advertisement