Phrogz
1/23/2008 10:06:00 PM
On Jan 23, 2:56 pm, Zenki Nine <amateurdesig...@gmail.com> wrote:
> Thanks All for helping me out on this..
>
> Now..here's another issue..Say i want to display the retired players
> last name in alphabetical order with its contents. Noticed this time
> some has middle names, initials.
>
> How would I tackle this?
>
> Sorry, I've looked thru many books and have a difficult time solving
> this..
>
> retired_players = <<PLAYERS
> Mike Anthony Gallego, 1985-1997
> Calvin Edwin Ripken, Jr., 1981-2001
> Tony K. Gwynn, 1982-2004
> Dennis Eckersley, 1975-1998
> PLAYERS
retired_players = <<PLAYERS
Mike Anthony Gallego, 1985-1997
Calvin Edwin Ripken, Jr., 1981-2001
Tony K. Gwynn, 1982-2004
Dennis Eckersley, 1975-1998
PLAYERS
# Break it into lines
players = retired_players.scan( /^.+/ )
# Turn each line into a real Player
Player = Struct.new( :name, :start_year, :end_year )
players.map!{ |line|
_, *pieces = line.match( /^(.+), (\d+)-(\d+)/ ).to_a
Player.new( *pieces )
}
# Sort as you see fit
players = players.sort_by{ |player|
# find the last name to use as the sort key
player.name[ /\w+(?:, (?:jr\.|sr\.|phd|dds))?$/i ]
}
puts players
#=> #<struct Player name="Dennis Eckersley", start_year="1975",
end_year="1998">
#=> #<struct Player name="Mike Anthony Gallego", start_year="1985",
end_year="1997">
#=> #<struct Player name="Tony K. Gwynn", start_year="1982",
end_year="2004">
#=> #<struct Player name="Calvin Edwin Ripken, Jr.",
start_year="1981", end_year="2001">