FAQ
I think this should be added
to: https://code.google.com/p/go-wiki/wiki/SliceTricks ??
On Thursday, September 22, 2011 11:43:05 PM UTC+2, Andrew Gerrand wrote:

// A data structure to hold a key/value pair.
type Pair struct {
Key string
Value int
}

// A slice of Pairs that implements sort.Interface to sort by Value.
type PairList []Pair
func (p PairList) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p PairList) Len() int { return len(p) }
func (p PairList) Less(i, j int) bool { return p[i].Value < p[j].Value }

// A function to turn a map into a PairList, then sort and return it.
func sortMapByValue(m map[string]int) PairList {
p := make(PairList, len(m))
i := 0
for k, v := range m {
p[i] = Pair{k, v}
}
sort.Sort(p)
return p
}
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJul 24, '14 at 10:08p
activeJul 24, '14 at 10:08p
posts1
users1
websitegolang.org

1 user in discussion

Gerald Stanje: 1 post

People

Translate

site design / logo © 2021 Grokbase