FAQ
Andrew, you forgot to i++ in that loop and it took me way longer to realise
than it should have. I think range has spoiled me. :P
On Friday, 23 September 2011 07:43:05 UTC+10, 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
postedAug 17, '14 at 2:01a
activeAug 17, '14 at 2:01a
posts1
users1
websitegolang.org

1 user in discussion

Anthony Voutas: 1 post

People

Translate

site design / logo © 2021 Grokbase