# Sort Array of Hashes

**URL:** <https://www.ruby-forum.com/t/sort-array-of-hashes/144232>\
**Category:** Ruby\
**Created:** [July 27, 2008, 4:49am UTC](https://www.ruby-forum.com/t/sort-array-of-hashes/144232 "2008-07-27T04:49:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![adler1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/858c86/32.png) [@adler1](https://www.ruby-forum.com/u/adler1)\
**Post date:** [July 27, 2008, 4:49am UTC](https://www.ruby-forum.com/t/sort-array-of-hashes/144232/1 "2008-07-27T04:49:05Z")

</div>

Hi all,

i wrote array of hashes and i would like to sort it. I search in the net  
few hours how to do this, but was not able to find exactly what i  
wanted.

Here is my code:  
\_FilesData = Array.new  
\_FilesData \<\< { :FileName =\> splitLine[8] ,  
:Year =\> Time.now.year ,  
:Month =\> month ,  
:Day =\> splitLine[6].to\_i ,  
:Hour =\> hm[0].to\_i ,  
:Min =\> hm[1].to\_i ,  
:Suffix =\> Str ;  
:Size =\> splitLine[4].to\_i  
}

I would like to sort \_FilesData accourding to Year, then Month, then  
Day, then by Suffix (Year, Month and Day are Integers numbers and Suffix  
is an ascii string).

Just a remark, the size of \_FilesData is more then one date set.

Thanks

---

<div class="post-metadata">

**Author:** ![adler1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/858c86/32.png) [@adler1](https://www.ruby-forum.com/u/adler1)\
**Post date:** [July 27, 2008, 4:59am UTC](https://www.ruby-forum.com/t/sort-array-of-hashes/144232/2 "2008-07-27T04:59:17Z")

</div>

Hi –

On Sun, 27 Jul 2008, Adgar M. wrote:

> Hi all,
> 
> i wrote array of hashes and i would like to sort it. I search in the net  
> few hours how to do this, but was not able to find exactly what i  
> wanted.
> 
> Here is my code:  
> \_FilesData = Array.new

\_FilesData is kind of a weird variable name.

> I would like to sort \_FilesData accourding to Year, then Month, then  
> Day, then by Suffix (Year, Month and Day are Integers numbers and Suffix  
> is an ascii string).
> 
> Just a remark, the size of \_FilesData is more then one date set.

Try this (untested):

\_FilesData.sort\_by {|hash| hash.values\_at(:Year, :Month, :Day,  
:Suffix) }

David
