FAQ
I think Andrew Gerrand's solution is the most idiomatic (or at least, it's
the one I came up with too :)
Is this still the best way to solve this problem? I love Go, but this seems
to be one of those (usually rare) cases where the Python version is 10x
more concise :(
On Thursday, September 22, 2011 2:43:05 PM UTC-7, 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/groups/opt_out.

Search Discussions

  • Rodrigo Kochenburger at Jul 10, 2013 at 5:57 pm
    I don't know why my other post got deleted, so here it is again:

    Go does not give you everything out-of-the-box, instead it gives you a
    concise and simple language to build from, so it's a language that
    exploring and applying algorithms and data structure is really well suited.
    That said, it looks to me that what you want is a binary search tree (or
    balanced ordered binary tree).

    It will have logarithmic insert/lookup time (hash map is constant), which
    should be good enough, but it will preserve order.

    Here are some 3rd party implementations:

    http://www.stathat.com/src/treap
    https://code.google.com/p/go-wiki/wiki/Projects#Trees

    Hope that helps.
    On Thursday, September 22, 2011 2:20:46 PM UTC-7, Miki Tebeka wrote:

    Assuming I have a map[string]int, what's the easiest way to sort the keys
    by their values?
    (In Python I'd do sorted(m, key=lambda k: m[k])).
    --
    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/groups/opt_out.
  • John Fries at Jul 10, 2013 at 9:19 pm
    Nicely reframed! :)
    I tend to be so wedded to manipulating slices and maps that I don't often
    think to use trees, but for my particular problem it looks like a better
    solution. I'll try it out.
    On Wednesday, July 10, 2013 10:57:24 AM UTC-7, Rodrigo Kochenburger wrote:

    I don't know why my other post got deleted, so here it is again:

    Go does not give you everything out-of-the-box, instead it gives you a
    concise and simple language to build from, so it's a language that
    exploring and applying algorithms and data structure is really well suited.
    That said, it looks to me that what you want is a binary search tree (or
    balanced ordered binary tree).

    It will have logarithmic insert/lookup time (hash map is constant), which
    should be good enough, but it will preserve order.

    Here are some 3rd party implementations:

    http://www.stathat.com/src/treap
    https://code.google.com/p/go-wiki/wiki/Projects#Trees

    Hope that helps.
    On Thursday, September 22, 2011 2:20:46 PM UTC-7, Miki Tebeka wrote:

    Assuming I have a map[string]int, what's the easiest way to sort the keys
    by their values?
    (In Python I'd do sorted(m, key=lambda k: m[k])).
    --
    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/groups/opt_out.
  • Rodrigo Kochenburger at Jul 10, 2013 at 10:53 pm
    Go is a language that does not give you everything out of the box but
    rather focus in a concise language to build from.

    That said, if you're looking for something that can give you fast lookup
    while preserving ordering, it seems to be that what you need is a binary
    search tree, or balanced ordered binary tree. The insert and lookup won't
    be as fast as a hash map but it will be logarithmic, and probably fast
    enough.

    There are a few implementations out there you can use:

    http://www.stathat.com/src/treap
    https://code.google.com/p/go-wiki/wiki/Projects#Trees

    Hope that helps.
    On Wednesday, July 10, 2013 9:04:03 AM UTC-7, John Fries wrote:

    I think Andrew Gerrand's solution is the most idiomatic (or at least, it's
    the one I came up with too :)
    Is this still the best way to solve this problem? I love Go, but this
    seems to be one of those (usually rare) cases where the Python version is
    10x more concise :(
    On Thursday, September 22, 2011 2:43:05 PM UTC-7, 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/groups/opt_out.
  • David DENG at Jul 10, 2013 at 11:12 pm
    If you consider Andrew's solution too verbose, here's a short one (of cause
    not so short as Python).

    var keys := make([]string, 0, len(m))
    for k := range(m) {
         keys := append(keys, k)
    }

    villa.SortF(len(keys),

         func(i, j int)bool { return m[keys[i] < m[keys[j]] },

         func(i, j) { keys[i], keys[j] = keys[j], keys[i] })



    villa.SortF is defined in githum.com/daviddengcn/go-villa, you can copy its
    source out if you don't want to import it.

    David
    On Friday, September 23, 2011 5:20:46 AM UTC+8, Miki Tebeka wrote:

    Assuming I have a map[string]int, what's the easiest way to sort the keys
    by their values?
    (In Python I'd do sorted(m, key=lambda k: m[k])).
    --
    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/groups/opt_out.
  • Kevin Gillette at Jul 11, 2013 at 1:57 am
    Even shorter would be:

    keys := make([]string, 0, len(m))
    for k := range m {
    keys = append(keys, k)
    }
    sort.Strings(keys)

    Of course this is key sorting. The OP was concerned with sorting the keys
    according to the values (essentially sorting-by-proxy), which is different
    from what has recently been demonstrated.
    On Wednesday, July 10, 2013 5:12:46 PM UTC-6, David DENG wrote:

    If you consider Andrew's solution too verbose, here's a short one (of
    cause not so short as Python).

    var keys := make([]string, 0, len(m))
    for k := range(m) {
    keys := append(keys, k)
    }

    villa.SortF(len(keys),

    func(i, j int)bool { return m[keys[i] < m[keys[j]] },

    func(i, j) { keys[i], keys[j] = keys[j], keys[i] })



    villa.SortF is defined in githum.com/daviddengcn/go-villa, you can copy
    its source out if you don't want to import it.

    David
    On Friday, September 23, 2011 5:20:46 AM UTC+8, Miki Tebeka wrote:

    Assuming I have a map[string]int, what's the easiest way to sort the keys
    by their values?
    (In Python I'd do sorted(m, key=lambda k: m[k])).
    --
    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/groups/opt_out.
  • David DENG at Jul 11, 2013 at 2:48 am
    If you are talking my code fragment, it sorts keys by corresponding values.
    But yours is not. What's your point?

    David
    On Thursday, July 11, 2013 9:57:25 AM UTC+8, Kevin Gillette wrote:

    Even shorter would be:

    keys := make([]string, 0, len(m))
    for k := range m {
    keys = append(keys, k)
    }
    sort.Strings(keys)

    Of course this is key sorting. The OP was concerned with sorting the keys
    according to the values (essentially sorting-by-proxy), which is different
    from what has recently been demonstrated.
    On Wednesday, July 10, 2013 5:12:46 PM UTC-6, David DENG wrote:

    If you consider Andrew's solution too verbose, here's a short one (of
    cause not so short as Python).

    var keys := make([]string, 0, len(m))
    for k := range(m) {
    keys := append(keys, k)
    }

    villa.SortF(len(keys),

    func(i, j int)bool { return m[keys[i]] < m[keys[j]] },

    func(i, j) { keys[i], keys[j] = keys[j], keys[i] })



    villa.SortF is defined in githum.com/daviddengcn/go-villa, you can copy
    its source out if you don't want to import it.

    David
    On Friday, September 23, 2011 5:20:46 AM UTC+8, Miki Tebeka wrote:

    Assuming I have a map[string]int, what's the easiest way to sort the
    keys by their values?
    (In Python I'd do sorted(m, key=lambda k: m[k])).
    --
    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/groups/opt_out.
  • Writeme at Sep 8, 2013 at 9:25 pm
    The gauntlet hath been thrown down.
    On Wednesday, July 10, 2013 9:48:16 PM UTC-5, David DENG wrote:

    If you are talking my code fragment, it sorts keys by corresponding
    values. But yours is not. What's your point?

    David
    On Thursday, July 11, 2013 9:57:25 AM UTC+8, Kevin Gillette wrote:

    Even shorter would be:

    keys := make([]string, 0, len(m))
    for k := range m {
    keys = append(keys, k)
    }
    sort.Strings(keys)

    Of course this is key sorting. The OP was concerned with sorting the keys
    according to the values (essentially sorting-by-proxy), which is different
    from what has recently been demonstrated.
    On Wednesday, July 10, 2013 5:12:46 PM UTC-6, David DENG wrote:

    If you consider Andrew's solution too verbose, here's a short one (of
    cause not so short as Python).

    var keys := make([]string, 0, len(m))
    for k := range(m) {
    keys := append(keys, k)
    }

    villa.SortF(len(keys),

    func(i, j int)bool { return m[keys[i]] < m[keys[j]] },

    func(i, j) { keys[i], keys[j] = keys[j], keys[i] })



    villa.SortF is defined in githum.com/daviddengcn/go-villa, you can copy
    its source out if you don't want to import it.

    David
    On Friday, September 23, 2011 5:20:46 AM UTC+8, Miki Tebeka wrote:

    Assuming I have a map[string]int, what's the easiest way to sort the
    keys by their values?
    (In Python I'd do sorted(m, key=lambda k: m[k])).
    --
    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/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJul 10, '13 at 4:04p
activeSep 8, '13 at 9:25p
posts8
users5
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase