Hash and Array sorting - a bit confused

Discussion in 'Ruby' started by Alpha Blue, Sep 2, 2009.

  1. Alpha Blue

    Alpha Blue Guest

    So, I have the following model method:

    def self.cupcake_report_list
    list = Schedule.find_all_by_division(2, :select =>
    'schedules.team_id, schedules.cupcake', :eek:rder => :team_id)
    cupcakes = []
    list.each do |row|
    teamname = ConferenceRecord.find_by_team_id(row.team_id, :joins
    => [:team, :conference])
    cupcakes << {'name'=> teamname.team.name,
    'teamid'=> teamname.team_id,
    'cupcake' => row.cupcake,
    'conference' => teamname.conference.short_name,
    'confid' => teamname.conference_id}
    end
    return cupcakes
    end

    This returns and array of hashes:

    120 rows: 0..119
    Each row containing 5 hash key/values

    Example:

    test = cupcake_report_list

    => {"name"=>"Georgia Tech", "cupcake"=>"Jacksonville St.",
    "conference"=>"ACC", "teamid"=>4, "confid"=>2}

    What I want to do is sort the entire array by the name hash key so that
    if I were to look within the array, it would like so:

    test[0]
    => {"name"=>"Air Force", etc., etc., etc., etc.}
    test[1]
    => {"name"=>"Akron", etc., etc., etc., etc.}
    test[2]
    => {"name"=>"Alabama", etc., etc., etc., etc.}

    etc..

    I can't figure out a way to do that correctly by a specific hash key
    within the array.

    Any ideas on how I can accomplish this?

    Many thanks.
    --
    Posted via http://www.ruby-forum.com/.
     
    Alpha Blue, Sep 2, 2009
    #1
    1. Advertising

  2. Alpha Blue

    7stud -- Guest

    Alpha Blue wrote:
    > Example:
    >
    > test = cupcake_report_list
    >
    > => {"name"=>"Georgia Tech", "cupcake"=>"Jacksonville St.",
    > "conference"=>"ACC", "teamid"=>4, "confid"=>2}
    >
    > What I want to do is sort the entire array by the name hash key so that
    > if I were to look within the array, it would like so:
    >
    > test[0]
    > => {"name"=>"Air Force", etc., etc., etc., etc.}
    > test[1]
    > => {"name"=>"Akron", etc., etc., etc., etc.}
    > test[2]
    > => {"name"=>"Alabama", etc., etc., etc., etc.}
    >
    > etc..
    >
    > I can't figure out a way to do that correctly by a specific hash key
    > within the array.
    >
    > Any ideas on how I can accomplish this?
    >
    > Many thanks.


    test = [
    {"name"=>"Zakron", "a" => "apple"},
    {"name"=>"Akron", "a" => "goodbye"},
    {"name"=>"Air Force", "a" => "hello"}
    ]

    result = test.sort_by do |hash|
    hash["name"]
    end

    p result

    --output:--
    [{"name"=>"Air Force", "a"=>"hello"}, {"name"=>"Akron", "a"=>"goodbye"},
    {"name"=>"Zakron", "a"=>"apple"}]

    result = test.sort_by do |hash|
    hash["name"]
    end

    p result

    --
    Posted via http://www.ruby-forum.com/.
     
    7stud --, Sep 2, 2009
    #2
    1. Advertising

  3. Alpha Blue

    Alpha Blue Guest

    Wow, why does it have to be so simple. :)

    Thanks a ton. If you knew what I was trying to do to get this to work,
    well, we'll pretend I didn't try all that stuff. :)

    Thanks mate.
    --
    Posted via http://www.ruby-forum.com/.
     
    Alpha Blue, Sep 2, 2009
    #3
    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. Hendrik Maryns

    Confused: Java6 32-bit vs. 64-bit

    Hendrik Maryns, May 2, 2007, in forum: Java
    Replies:
    3
    Views:
    596
    Tom Hawtin
    May 2, 2007
  2. Wanderer
    Replies:
    1
    Views:
    620
    Wanderer
    Mar 24, 2011
  3. rp
    Replies:
    1
    Views:
    591
    red floyd
    Nov 10, 2011
  4. gga
    Replies:
    6
    Views:
    150
    Stefan Lang
    Feb 17, 2005
  5. Srijayanth Sridhar
    Replies:
    19
    Views:
    674
    David A. Black
    Jul 2, 2008
Loading...

Share This Page