Alphabetizing

Z

Zenki Nine

Hi,

I'm trying to write a code where I can alphabetize the players last_name
with its batting average

players_avg= << PLAYERS
Barry Bonds, .293
Mike Gallego, .263
Ichiro Suzuki, .396
Tony Gwynn, .403
Alex Rodriguez, .310
PLAYERS

players = presidents_avg.strip.collect { |players| players.strip
}.compact

#initialiating players_by_last_name with an empty array
players_by_last_name = []
 
7

7stud --

Zenki said:
Hi,

I'm trying to write a code where I can alphabetize the players last_name
with its batting average

players_avg= << PLAYERS
Barry Bonds, .293
Mike Gallego, .263
Ichiro Suzuki, .396
Tony Gwynn, .403
Alex Rodriguez, .310
PLAYERS

players = presidents_avg.strip.collect { |players| players.strip
}.compact

#initialiating players_by_last_name with an empty array
players_by_last_name = []

Try this:

players_avg = <<PLAYERS
Barry Bonds, .293
Mike Gallego, .263
Ichiro Suzuki, .396
Tony Gwynn, .403
Alex Rodriguez, .310
PLAYERS

lines = players_avg.split("\n")
player_data = []

#chop lines into pieces
for line in lines
data = line.split(',')
names = data[0].split()
avg = data[1]
player_data << [names[0], names[1], avg]
end

#sort array by 2nd element, i.e. last name:
results = player_data.sort() do |arr1, arr2|
arr1[1]<=>arr2[1]
end

#output data:
results.each() do |arr|
puts "%s, %s %s" % [arr[1], arr[0], arr[2]]
end

--output:--
Bonds, Barry .293
Gallego, Mike .263
Gwynn, Tony .403
Rodriguez, Alex .310
Suzuki, Ichiro .396
 
M

Michael Fellinger

Hi,

I'm trying to write a code where I can alphabetize the players last_name
with its batting average

players_avg= << PLAYERS
Barry Bonds, .293
Mike Gallego, .263
Ichiro Suzuki, .396
Tony Gwynn, .403
Alex Rodriguez, .310
PLAYERS

csv = CSV.parse(players_avg)
sorted = csv.sort_by{|p,a| p.split.last }
sorted.each{|p,a| puts("%20s: %5s" % [p, a]) }
 
T

Todd Benson

Hi,

I'm trying to write a code where I can alphabetize the players last_name
with its batting average

players_avg= << PLAYERS
Barry Bonds, .293
Mike Gallego, .263
Ichiro Suzuki, .396
Tony Gwynn, .403
Alex Rodriguez, .310
PLAYERS

players = presidents_avg.strip.collect { |players| players.strip
}.compact

#initialiating players_by_last_name with an empty array
players_by_last_name = []

This isn't the best solution ... just there for s**ts and giggles...

irb(main):002:0> players = players_avg.split("\n").sort_by {|i|
(i.scan /\w*/)[2]}

Todd
 
Z

Zenki Nine

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

Thanks,
 
P

Phrogz

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">
 
P

Phrogz

Slightly simpler than my previous:

retired_players = <<PLAYERS
Mike Anthony Gallego, 1985-1997
Calvin Edwin Ripken, Jr., 1981-2001
Tony K. Gwynn, 1982-2004
Dennis Eckersley, 1975-1998
PLAYERS

# Find player info: yields an array of arrays
players = retired_players.scan( /^(.+), (\d+)-(\d+)/ )

players = players.sort_by{ |name, start_year, end_year|
# find the last name to use as the sort key
name[ /\w+(?:, (?:jr\.|sr\.|phd|dds))?$/i ]
}

p players
#=> [["Dennis Eckersley", "1975", "1998"], ["Mike Anthony Gallego",
"1985", "1997"], ["Tony K. Gwynn", "1982", "2004"], ["Calvin Edwin
Ripken, Jr.", "1981", "2001"]]
 
Z

Zenki Nine

Gavin said:
Slightly simpler than my previous:

retired_players = <<PLAYERS
Mike Anthony Gallego, 1985-1997
Calvin Edwin Ripken, Jr., 1981-2001
Tony K. Gwynn, 1982-2004
Dennis Eckersley, 1975-1998
PLAYERS

# Find player info: yields an array of arrays
players = retired_players.scan( /^(.+), (\d+)-(\d+)/ )

players = players.sort_by{ |name, start_year, end_year|
# find the last name to use as the sort key
name[ /\w+(?:, (?:jr\.|sr\.|phd|dds))?$/i ]
}

p players
#=> [["Dennis Eckersley", "1975", "1998"], ["Mike Anthony Gallego",
"1985", "1997"], ["Tony K. Gwynn", "1982", "2004"], ["Calvin Edwin
Ripken, Jr.", "1981", "2001"]]

Thanks. Is there a way where I can display the full tenure? ie.
"1975-1998" instead?

I want to display in full:

'Dennis Eckersley, 1975-1998'
 
L

Lorenzo E. Danielsson

Zenki said:
Gavin said:
Slightly simpler than my previous:

retired_players = <<PLAYERS
Mike Anthony Gallego, 1985-1997
Calvin Edwin Ripken, Jr., 1981-2001
Tony K. Gwynn, 1982-2004
Dennis Eckersley, 1975-1998
PLAYERS

# Find player info: yields an array of arrays
players = retired_players.scan( /^(.+), (\d+)-(\d+)/ )

players = players.sort_by{ |name, start_year, end_year|
# find the last name to use as the sort key
name[ /\w+(?:, (?:jr\.|sr\.|phd|dds))?$/i ]
}

p players
#=> [["Dennis Eckersley", "1975", "1998"], ["Mike Anthony Gallego",
"1985", "1997"], ["Tony K. Gwynn", "1982", "2004"], ["Calvin Edwin
Ripken, Jr.", "1981", "2001"]]

Thanks. Is there a way where I can display the full tenure? ie.
"1975-1998" instead?

I want to display in full:

'Dennis Eckersley, 1975-1998'

Replace:

p players

with:

players.each { |p, s, e|
puts "#{p}, #{s}-#{e}"
}
 
7

7stud --

Zenki said:
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

Thanks,


My attempt:


retired_players = <<PLAYERS
Mike Anthony Gallego, 1985-1997
Calvin Edwin Ripken, Jr., 1981-2001
Tony K. Gwynn, 1982-2004
Dennis Eckersley, 1975-1998
PLAYERS

#A container for each player's data:
Player = Struct.new:)last, :first_middle, :years)

#An array to hold each Player:
players = []

#Divide the string into lines:
lines = retired_players.split("\n")
lines.each do |line|
pieces = line.split(',')

#Extract the years played:
years = pieces[-1].strip()

#Extract the title, e.g. .jr, .sr
if pieces.length == 3 #then player has a title like .jr, .sr
title = pieces[1].strip()
else
title = false
end

#Extract the names:
names = pieces[0].split()
last = names[-1]
first_middle = names[0..-2].join(" ")
if title
last = sprintf("%s %s", last, title)
end

#Create a new Player with the above data:
players << Player.new(last, first_middle, years)
end

#Sort the Player objects in the players array by last name:
sorted_players = players.sort do |p1, p2|
p1.last<=>p2.last
end

#Output the sorted players:
sorted_players.each do |player|
puts sprintf("%s, %s %s", player.last, player.first_middle,
player.years)
end


--output:--
Eckersley, Dennis 1975-1998
Gallego, Mike Anthony 1985-1997
Gwynn, Tony K. 1982-2004
Ripken Jr., Calvin Edwin 1981-2001
 
T

Todd Benson

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

Thanks,

If you cannot identify the last name by position -- I don't know --
then maybe you need to use a database. I suppose you can grab the
first written word before a comma, but I'm not sure I'd trust that.

Todd
 
T

Todd Benson

My golf attempt. Please don't use this for production code...

players_avg = <<PLAYERS
Mike Anthony Gallego, 1985-1997
Calvin Edwin Ripken, Jr., 1981-2001
Tony K. Gwynn, 1982-2004
Dennis Eckersley, 1975-1998
PLAYERS

puts players = players_avg.split("\n").sort_by {|i| i.scan /\w*?,/}

Todd
 

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

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top