# (no subject)

**URL:** <https://www.ruby-forum.com/t/no-subject/91447>\
**Category:** Ruby\
**Created:** [April 9, 2007, 12:02pm UTC](https://www.ruby-forum.com/t/no-subject/91447 "2007-04-09T12:02:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![john\_old](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9f8e36/32.png) [@john\_old](https://www.ruby-forum.com/u/john_old)\
**Post date:** [April 9, 2007, 12:02pm UTC](https://www.ruby-forum.com/t/no-subject/91447/1 "2007-04-09T12:02:22Z")

</div>

Why does this seem to sort ascending alphabetical?

puts ({ :shoe\_size =\> 29.5, :name =\> ‘Fred’, :age =\> 10, :size =\>  
‘L’ }).to\_s

sizeLshoe\_size29.5nameFredage10

---

<div class="post-metadata">

**Author:** ![john\_old](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9f8e36/32.png) [@john\_old](https://www.ruby-forum.com/u/john_old)\
**Post date:** [April 9, 2007, 12:23pm UTC](https://www.ruby-forum.com/t/no-subject/91447/2 "2007-04-09T12:23:24Z")

</div>

On 4/9/07, John J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Why does this seem to sort ascending alphabetical?
> 
> puts ({ :shoe\_size =\> 29.5, :name =\> ‘Fred’, :age =\> 10, :size =\>  
> ‘L’ }).to\_s
> 
> sizeLshoe\_size29.5nameFredage10

I’ll handle this in two parts:  
First what you are doing:  
Actually all you are doing is  
puts ({ :shoe\_size =\> 29.5, :name =\> ‘Fred’, :age =\> 10, :size =\>  
‘L’ })

The to\_s applied to the return value of puts, which is nil is the  
empty string which is discarded.

I guess that you want to get a nice representation of the hash, try  
puts( { :shoe\_size =\> 29.5, :name =\> ‘Fred’, :age =\> 10, :size =\> ‘L’  
}.inspect )

Please note that there should not be any spaces bewteen “puts” and “(”  
as this is deprecated.

Now what you were asking:  
The order of the string representation of a Hash is defined as the  
order of the Hash, which means it is not. It is completely up to the  
interpreter how to iterate over a hash, how to convert it to an array  
or to a string.  
AFAIK these operations could even be inconsistent if that suited an  
interpreter.

Use this hash to see that it is not necessarily in alphabetic descending  
order  
{:a=\>42, :b=\>43, :c=\>44}, but be aware that it might be if you use a  
different interpreter than I do  
irb(main):036:0\> RUBY\_VERSION  
=\> “1.8.5”

Cheers  
Robert

---

<div class="post-metadata">

**Author:** ![john\_old](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9f8e36/32.png) [@john\_old](https://www.ruby-forum.com/u/john_old)\
**Post date:** [April 9, 2007, 12:39pm UTC](https://www.ruby-forum.com/t/no-subject/91447/3 "2007-04-09T12:39:25Z")

</div>

well, actually, I knew this part about hashes being unordered.  
Which, is why I asked.  
I was just wondering if there was some kind of implicit sort  
occurring. I was unable to find anything in the pickaxe book that  
would indicate any sort occurring here.  
I’m on Ruby 1.8.4 locally.  
I’m waiting to upgrade until the version that ships with the next OS X.  
Far too much hassle.

Anyway, I’ve been working through all of the new book “Beginning  
Ruby, from Novice to Professional” and this example came from there.  
It kind of threw me, because as I added things to the hash, they  
seemed to stay in some order, not just first in/last out, pop/push  
standard stuff.  
Maybe my irb is alive and has a sense of humor.

---

<div class="post-metadata">

**Author:** ![john\_old](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9f8e36/32.png) [@john\_old](https://www.ruby-forum.com/u/john_old)\
**Post date:** [April 9, 2007, 1:59pm UTC](https://www.ruby-forum.com/t/no-subject/91447/4 "2007-04-09T13:59:31Z")

</div>

On 4/9/07, John J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> well, actually, I knew this part about hashes being unordered.  
> Which, is why I asked.  
> I was just wondering if there was some kind of implicit sort  
> occurring.

by right, u shouldnt really care, since it is, after all, a hash (but  
of course we all all curious animals… ;))

I would probably say the same thing as Robert - there is no “standard”  
here, and it is all up to the interpreter to do as it wishes.

If u really wish to sort, u could use use ‘.sort’, or ‘.sort\_by’

{5=\>:a, 7=\>:a, 2=\>:a}.sort  
{:z=\>42, :d=\>43, :c=\>44}.sort\_by {|a| a[0].to\_s}

although of course, this also has the effect of transforming the hash  
into an array…

-jf

–  
“It’s so hard to write a graphics driver that open-sourcing it would not  
help.”  
– Andrew Fear, Software Product Manager, NVIDIA Corporation

> **[An Interview With Linus Torvalds: Linux and Git - Part 1 30 Years Of Linux](https://www.tag1consulting.com/blog/interview-linus-torvalds-linux-and-git)**
>
> Thirty years ago, Linus Torvalds was a 21 year old student at the University of Helsinki when he first released the Linux Kernel. His announcement started, “I’m doing a (free) operating system (just a hobby, won't be big and professional…)”. Three...
