package main
import (
"fmt"
"sort"
)
func main() {
mymap := map[string]int{
"Tivi": 230,
"Laptop": 1200,
"Mobile": 500,
"Computer": 98,
}
keys := make([]string, 0, len(mymap))
for key := range mymap {
keys = append(keys, key)
}
sort.Slice(keys, func(i, j int) bool {
return mymap[keys[i]] > mymap[keys[j]]
})
for _, key := range keys {
fmt.Printf("%-7v %v\n", key, mymap[key])
}
}
Output
Laptop 1200
Mobile 500
Tivi 230
Computer 98