如何按值对map [int] int进行排序? [重复]

This question already has an answer here:

My insertion and quicksort does not work on map[uint64]uint64 values. Can anyone help? Thanks in advance. Would like to have sort of the map "aint" by value.

Please ask for details if any. I will improve on this question. Thanks again.

package main

import (
    "sort"
    "fmt"
    "time"
    "runtime"
    "math/rand"
)
    func main() {

    runtime.GOMAXPROCS(runtime.NumCPU())
    start := time.Now()

    //the map variable
    aint := map[uint64]uint64{}

    start = time.Now()
    for i := uint64(0); i < 100000000; i++ {
            aint[i+32132112313] = uint64(rand.Intn(13123123123)+2312423213) //random generation of input data
//              aint = insertSort(aint,uint64(rand.Intn(13123123123)))
    }
    fmt.Printf("%d
", aint[22] )

    elapsed := time.Since(start)
    fmt.Printf("Entry took %s %d
", elapsed)

    start = time.Now()
    quicksort(aint)

    //      sort.Sort(sort.IntSlice(aint))

    elapsed = time.Since(start)
    fmt.Printf("Sorting took %s %d
", elapsed)
}
func insertionsort(items []int) {
    var n = len(items)
    for i := 1; i < n; i++ {
            j := i
            for j > 0 {
                    if items[j-1] > items[j] {
                            items[j-1], items[j] = items[j], items[j-1]
                    }
                    j = j - 1
            }
    }
}


func quicksort(a map[uint64]uint64) map[uint64]uint64 {
    if len(a) < 2 {
            return a
    }

    left, right := uint64(0), uint64(len(a)-1)

    pivot := Uint64() % uint64(len(a))

    a[pivot], a[right] = a[right], a[pivot]
    // does not work anymore from here onwards.
    for uint64(i), _ := range a {
            if a[i] < a[right] {
                    a[left], a[i] = a[i], a[left]
                    left++
            }
    }

    a[left], a[right] = a[right], a[left]

    quicksort(a[:left])
    quicksort(a[left+1:])

    return a
}
</div>

Go maps are unordered data structures. No matter what you do it will not be sorted, either by key or by value.

If you want to sort your values, you should use a slice and perform your quicksort over that.

See the Go spec or Effective Go for more details.