PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Sort hashes using values
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Sort hashes using values

Réponse
 
LinkBack Outils de la discussion
Vieux 10/03/2008, 17h32   #1
Subbu
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Sort hashes using values

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}
  Réponse avec citation
Vieux 10/03/2008, 17h47   #2
Rodrigo Bermejo
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sort hashes using values

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/.

  Réponse avec citation
Vieux 10/03/2008, 18h41   #3
Robert Klemme
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sort hashes using values

2008/3/10, Rodrigo Bermejo <rodrigo.bermejo@ps.ge.com>:
> 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

  Réponse avec citation
Vieux 10/03/2008, 18h48   #4
Todd Benson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sort hashes using values

On Mon, Mar 10, 2008 at 11:34 AM, Subbu <subramani.athikunte@gmail.com> 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

  Réponse avec citation
Vieux 11/03/2008, 07h09   #5
Subbu
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sort hashes using values

On Mar 10, 10:48 am, Todd Benson <caduce...@gmail.com> wrote:
> On Mon, Mar 10, 2008 at 11:34 AM, Subbu <subramani.athiku...@gmail.com> 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.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 17h31.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15126 seconds with 13 queries