Sorting keys of hash based on value

Discussion in 'Ruby' started by Aldric Giacomoni, Mar 22, 2010.

  1. Let's say we have this contrived example:

    hash = {:a => {:happy => 5},
    :b => {:happy => 4},
    :c => {:happy => 7}
    }

    I would like to get the keys sorted by descending :happy value, like so:

    [:c, :a, :b]

    How would I do this?
    The best I've come up with is this:

    irb(main):018:0> hash.sort_by { |x, y| -y[:happy] }
    => [[:c, {:happy=>7}], [:a, {:happy=>5}], [:b, {:happy=>4}]]

    It's not elegant :(
    --
    Posted via http://www.ruby-forum.com/.
    Aldric Giacomoni, Mar 22, 2010
    #1
    1. Advertising

  2. Aldric Giacomoni wrote:
    > Let's say we have this contrived example:
    >
    > hash = {:a => {:happy => 5},
    > :b => {:happy => 4},
    > :c => {:happy => 7}
    > }
    >
    > I would like to get the keys sorted by descending :happy value, like so:
    >
    > [:c, :a, :b]
    >
    > How would I do this?
    > The best I've come up with is this:
    >
    > irb(main):018:0> hash.sort_by { |x, y| -y[:happy] }
    > => [[:c, {:happy=>7}], [:a, {:happy=>5}], [:b, {:happy=>4}]]
    >
    > It's not elegant :(
    >

    Do you want keys or the whole hash?

    irb(main):015:0> hash.keys.sort { |a,b| hash[:happy] <=>
    hash[a][:happy] }
    => [:c, :a, :b]
    Riccardo Cecolin, Mar 22, 2010
    #2
    1. Advertising

  3. Riccardo Cecolin wrote:
    >>

    > Do you want keys or the whole hash?
    >
    > irb(main):015:0> hash.keys.sort { |a,b| hash[:happy] <=>
    > hash[a][:happy] }
    > => [:c, :a, :b]


    Of course.. I was trying to make it too complicated. Thanks!
    --
    Posted via http://www.ruby-forum.com/.
    Aldric Giacomoni, Mar 22, 2010
    #3
  4. On Mar 22, 2010, at 2:40 PM, Aldric Giacomoni wrote:

    > Riccardo Cecolin wrote:
    >>>

    >> Do you want keys or the whole hash?
    >>
    >> irb(main):015:0> hash.keys.sort { |a,b| hash[:happy] <=>
    >> hash[a][:happy] }
    >> => [:c, :a, :b]

    >
    > Of course.. I was trying to make it too complicated. Thanks!
    >


    Or the slightly simpler sort_by

    hash.keys.sort_by {|k| hast[k][:happy] }

    If the hash.size is large, this can be a big performance win, but with
    just three keys, you won't notice any difference.

    -Rob

    Rob Biedenharn http://agileconsultingllc.com
    Rob Biedenharn, Mar 22, 2010
    #4
  5. On 03/22/2010 08:49 PM, Rob Biedenharn wrote:
    > On Mar 22, 2010, at 2:40 PM, Aldric Giacomoni wrote:
    >
    >> Riccardo Cecolin wrote:
    >>> Do you want keys or the whole hash?
    >>>
    >>> irb(main):015:0> hash.keys.sort { |a,b| hash[:happy] <=>
    >>> hash[a][:happy] }
    >>> => [:c, :a, :b]

    >> Of course.. I was trying to make it too complicated. Thanks!
    >>

    >
    > Or the slightly simpler sort_by
    >
    > hash.keys.sort_by {|k| hast[k][:happy] }


    Just for the variety:

    hash.sort_by {|k,v| -v[:happy]}.map(&:first)

    Kind regards

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Mar 22, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vikas
    Replies:
    3
    Views:
    510
    CBFalconer
    Aug 16, 2007
  2. rp
    Replies:
    1
    Views:
    512
    red floyd
    Nov 10, 2011
  3. Gunnar Hjalmarsson

    Sorting "string" numerical keys from a hash.

    Gunnar Hjalmarsson, Sep 4, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    103
    Joe Smith
    Sep 6, 2004
  4. anthony
    Replies:
    5
    Views:
    113
    J├╝rgen Exner
    Dec 9, 2005
  5. Rasmus Villemoes

    Sorting based on existence of keys

    Rasmus Villemoes, Feb 19, 2009, in forum: Perl Misc
    Replies:
    20
    Views:
    263
Loading...

Share This Page