sort Array of Hashes

A

Adgar Marks

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
 
D

David A. Black

Hi --

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

Try this (untested):

_FilesData.sort_by {|hash| hash.values_at:)Year, :Month, :Day, :Suffix) }


David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top