Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Sort hashes using values

Reply
Thread Tools

Sort hashes using values

 
 
Subbu
Guest
Posts: n/a
 
      03-10-2008
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}
 
Reply With Quote
 
 
 
 
Rodrigo Bermejo
Guest
Posts: n/a
 
      03-10-2008
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/.

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      03-10-2008
2008/3/10, Rodrigo Bermejo <(E-Mail Removed)>:
> 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

 
Reply With Quote
 
Todd Benson
Guest
Posts: n/a
 
      03-10-2008
On Mon, Mar 10, 2008 at 11:34 AM, Subbu <(E-Mail Removed)> 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

 
Reply With Quote
 
Subbu
Guest
Posts: n/a
 
      03-11-2008
On Mar 10, 10:48 am, Todd Benson <(E-Mail Removed)> wrote:
> On Mon, Mar 10, 2008 at 11:34 AM, Subbu <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
using hashes as keys in hashes Steven Arnold Ruby 3 11-23-2005 03:25 PM
How does one sort hash of hashes by multiple values? joe.yakich@gmail.com Ruby 3 11-05-2005 02:51 AM
Hash of hashes, of hashes, of arrays of hashes Tim O'Donovan Perl Misc 5 10-28-2005 05:59 AM
Hashes of Hashes via subs Ben Holness Perl 8 10-08-2003 06:57 AM
Sort a hash based on values in the hash stored as arrays of hashes Tore Aursand Perl Misc 3 09-16-2003 10:14 AM



Advertisments