To sort a map by keys in Go, you can extract the keys, sort them, and then iterate over the sorted keys to access the values. This method provides a way to display map entries in a sorted order.
We can sort a map by keys in Go by extracting the keys into a slice, sorting the slice, and then iterating over the sorted keys to access the map values.
For example,
fmt
and sort
packages, which provide the necessary functions for input-output operations and sorting.myMap
with some key-value pairs. In this example, the map has integer keys and string values.keys
.sort.Ints
function.fmt.Println
function.package main
import (
"fmt"
"sort"
)
func main() {
// Declare and initialize a map
myMap := map[int]string{
3: "three",
1: "one",
2: "two",
}
// Extract the keys and store them in a slice
keys := make([]int, 0, len(myMap))
for key := range myMap {
keys = append(keys, key)
}
// Sort the slice of keys
sort.Ints(keys)
// Iterate over the sorted keys and print the key-value pairs
fmt.Println("Sorted Map by Keys:")
for _, key := range keys {
fmt.Printf("%d: %s\n", key, myMap[key])
}
}
Sorted Map by Keys: 1: one 2: two 3: three
We can sort a map by keys in descending order in Go by extracting the keys into a slice, sorting the slice in reverse order, and then iterating over the sorted keys to access the map values.
For example,
fmt
and sort
packages, which provide the necessary functions for input-output operations and sorting.myMap
with some key-value pairs. In this example, the map has integer keys and string values.keys
.sort.Sort
function with a custom sort interface.fmt.Println
function.package main
import (
"fmt"
"sort"
)
func main() {
// Declare and initialize a map
myMap := map[int]string{
3: "three",
1: "one",
2: "two",
}
// Extract the keys and store them in a slice
keys := make([]int, 0, len(myMap))
for key := range myMap {
keys = append(keys, key)
}
// Sort the slice of keys in descending order
sort.Sort(sort.Reverse(sort.IntSlice(keys)))
// Iterate over the sorted keys and print the key-value pairs
fmt.Println("Sorted Map by Keys (Descending):")
for _, key := range keys {
fmt.Printf("%d: %s\n", key, myMap[key])
}
}
Sorted Map by Keys (Descending): 3: three 2: two 1: one
We can sort a map by string keys in Go by extracting the keys into a slice, sorting the slice, and then iterating over the sorted keys to access the map values.
For example,
fmt
and sort
packages, which provide the necessary functions for input-output operations and sorting.myMap
with some key-value pairs. In this example, the map has string keys and integer values.keys
.sort.Strings
function.fmt.Println
function.package main
import (
"fmt"
"sort"
)
func main() {
// Declare and initialize a map
myMap := map[string]int{
"three": 3,
"one": 1,
"two": 2,
}
// Extract the keys and store them in a slice
keys := make([]string, 0, len(myMap))
for key := range myMap {
keys = append(keys, key)
}
// Sort the slice of keys
sort.Strings(keys)
// Iterate over the sorted keys and print the key-value pairs
fmt.Println("Sorted Map by Keys:")
for _, key := range keys {
fmt.Printf("%s: %d\n", key, myMap[key])
}
}
Sorted Map by Keys: one: 1 two: 2 three: 3
In this tutorial, we learned How to Sort a Map by Keys in Go language with well detailed examples.