Google Groups Home
Help | Sign in
sort Array of Hashes
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  2 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Adgar Marks  
View profile
 More options Jul 26, 10:42 pm
Newsgroups: comp.lang.ruby
From: Adgar Marks <adgar.ma...@yahoo.de>
Date: Sat, 26 Jul 2008 21:42:56 -0500
Local: Sat, Jul 26 2008 10:42 pm
Subject: sort Array of Hashes
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
--
Posted via http://www.ruby-forum.com/.


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
David A. Black  
View profile
 More options Jul 26, 10:52 pm
Newsgroups: comp.lang.ruby
From: "David A. Black" <dbl...@rubypal.com>
Date: Sat, 26 Jul 2008 21:52:15 -0500
Local: Sat, Jul 26 2008 10:52 pm
Subject: Re: sort Array of Hashes
Hi --

On Sun, 27 Jul 2008, Adgar Marks 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.

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

--
Rails training from David A. Black and Ruby Power and Light:
  *  Advancing With Rails    August 18-21    Edison, NJ
  * Co-taught by D.A. Black and Erik Kastner
See http://www.rubypal.com for details and updates!


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google