Sort hashes using values

Discussion in 'Ruby' started by Subbu, Mar 10, 2008.

  1. Subbu

    Subbu Guest

    Ruby automatically sorts hashes by keys, which means:

    >> h = {"first"=>2,"second"=>1,"third"=>3}

    => {"third"=>3, "second"=>1, "first"=>2}

    How do I sort this by the values? So that I have:
    {"second"=>1, "first"=>2, "third"=>3}
     
    Subbu, Mar 10, 2008
    #1
    1. Advertising

  2. Subbu wrote:
    > Ruby automatically sorts hashes by keys, which means:
    >
    >>> h = {"first"=>2,"second"=>1,"third"=>3}

    > => {"third"=>3, "second"=>1, "first"=>2}
    >
    > How do I sort this by the values? So that I have:
    > {"second"=>1, "first"=>2, "third"=>3}


    -------------> http://www.ruby-doc.org/core/classes/Hash.html
    hsh.sort => array
    hsh.sort {| a, b | block } => array

    Converts hsh to a nested array of [ key, value ] arrays and sorts it,
    using Array#sort.

    h = { "a" => 20, "b" => 30, "c" => 10 }
    h.sort #=> [["a", 20], ["b", 30], ["c", 10]]
    h.sort {|a,b| a[1]<=>b[1]} #=> [["c", 10], ["a", 20], ["b", 30]]

    --
    Posted via http://www.ruby-forum.com/.
     
    Rodrigo Bermejo, Mar 10, 2008
    #2
    1. Advertising

  3. 2008/3/10, Rodrigo Bermejo <>:
    > Subbu wrote:
    > > Ruby automatically sorts hashes by keys, which means:
    > >
    > >>> h = {"first"=>2,"second"=>1,"third"=>3}

    > > => {"third"=>3, "second"=>1, "first"=>2}
    > >
    > > How do I sort this by the values? So that I have:
    > > {"second"=>1, "first"=>2, "third"=>3}

    >
    >
    > -------------> http://www.ruby-doc.org/core/classes/Hash.html
    > hsh.sort => array
    > hsh.sort {| a, b | block } => array
    >
    > Converts hsh to a nested array of [ key, value ] arrays and sorts it,
    > using Array#sort.
    >
    > h = { "a" => 20, "b" => 30, "c" => 10 }
    > h.sort #=> [["a", 20], ["b", 30], ["c", 10]]
    > h.sort {|a,b| a[1]<=>b[1]} #=> [["c", 10], ["a", 20], ["b", 30]]


    and with #sort_by

    irb(main):001:0> {"first"=>2,"second"=>1,"third"=>3}.sort_by {|k,v| v}
    => [["second", 1], ["first", 2], ["third", 3]]

    Kind regards

    robert

    --
    use.inject do |as, often| as.you_can - without end
     
    Robert Klemme, Mar 10, 2008
    #3
  4. Subbu

    Todd Benson Guest

    On Mon, Mar 10, 2008 at 11:34 AM, Subbu <> wrote:
    > Ruby automatically sorts hashes by keys, which means:
    >
    > >> h = {"first"=>2,"second"=>1,"third"=>3}

    > => {"third"=>3, "second"=>1, "first"=>2}
    >
    > How do I sort this by the values? So that I have:
    > {"second"=>1, "first"=>2, "third"=>3}


    A hash, by nature, is not really sorted IIRC. If you sort, you need
    an Array object as a return value, which means using #sort_by...

    h = Hash["first", 2, "second", 1, "third", 3]
    h.sort_by {|k, v| v}

    It will give you an array of arrays.

    Todd
     
    Todd Benson, Mar 10, 2008
    #4
  5. Subbu

    Subbu Guest

    On Mar 10, 10:48 am, Todd Benson <> wrote:
    > On Mon, Mar 10, 2008 at 11:34 AM, Subbu <> wrote:
    > > Ruby automatically sorts hashes by keys, which means:

    >
    > > >> h = {"first"=>2,"second"=>1,"third"=>3}

    > > => {"third"=>3, "second"=>1, "first"=>2}

    >
    > > How do I sort this by the values? So that I have:
    > > {"second"=>1, "first"=>2, "third"=>3}

    >
    > A hash, by nature, is not really sorted IIRC. If you sort, you need
    > an Array object as a return value, which means using #sort_by...
    >
    > h = Hash["first", 2, "second", 1, "third", 3]
    > h.sort_by {|k, v| v}
    >
    > It will give you an array of arrays.
    >
    > Todd


    Thanks so much guys.
     
    Subbu, Mar 11, 2008
    #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. Ben Holness

    Hashes of Hashes via subs

    Ben Holness, Oct 5, 2003, in forum: Perl
    Replies:
    8
    Views:
    579
    Ben Holness
    Oct 8, 2003
  2. Replies:
    3
    Views:
    220
    William James
    Nov 5, 2005
  3. Steven Arnold

    using hashes as keys in hashes

    Steven Arnold, Nov 23, 2005, in forum: Ruby
    Replies:
    3
    Views:
    176
    Mauricio Fernández
    Nov 23, 2005
  4. Tore Aursand
    Replies:
    3
    Views:
    567
    Anno Siegel
    Sep 16, 2003
  5. Tim O'Donovan

    Hash of hashes, of hashes, of arrays of hashes

    Tim O'Donovan, Oct 27, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    223
Loading...

Share This Page