sort Array of Hashes

Discussion in 'Ruby' started by Adgar Marks, Jul 27, 2008.

  1. Adgar Marks

    Adgar Marks Guest

    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/.
     
    Adgar Marks, Jul 27, 2008
    #1
    1. Advertising

  2. 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!
     
    David A. Black, Jul 27, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Holness

    Hashes of Hashes via subs

    Ben Holness, Oct 5, 2003, in forum: Perl
    Replies:
    8
    Views:
    569
    Ben Holness
    Oct 8, 2003
  2. Laurent Colloud

    Consecutive sort on Array of Hashes

    Laurent Colloud, Aug 14, 2006, in forum: Ruby
    Replies:
    5
    Views:
    137
    Laurent Colloud
    Aug 15, 2006
  3. kazaam
    Replies:
    12
    Views:
    278
    Matthias Wächter
    Sep 13, 2007
  4. Matt Brooks
    Replies:
    16
    Views:
    228
    Matt Brooks
    Sep 16, 2009
  5. Tim O'Donovan

    Hash of hashes, of hashes, of arrays of hashes

    Tim O'Donovan, Oct 27, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    217
Loading...

Share This Page