David, the general idea of what you have to do to achieve your goals is to first treat the data:
Read data from data.csv file
Drop the first two lines out
Convert each String to an Array with 3 Strings
Filter the data you want: Leave the name of the people, convert the String with the date to an object of type Date, send the numbers out.
Once the data is filtered, we just need to sort / group it the way we want.
The snippet that solves your problem is as follows:
require 'date'
# Ler e formatar dados
formated_data = File.readlines('data.csv')[2..-1]
.map { |line| line.split() }
.map { |fields| [fields[0], Date.parse(fields[1])] }
# Agrupar e ordenar dados
# Maior data:
p formated_data.max_by{ |fields| fields[1] }
# Menor data:
p formated_data.min_by{ |fields| fields[1] }
# Pessoas nasceram na mesma data
p formated_data.group_by { |fields| fields[1] }
Now let's see what each line does:
Now that you have the data transformed into objects, it's easy to transform them so that they group / filter the way they answer your questions:
To get the maximum date, just say 'you want the maximum of the array with the #max_by
method and apply it to the second array field (which has the date). Docs: max_by
Same thing to get the least, but use the #min_by
method instead. Docs: min_by
Finally to group by date, use the #group_by method. Docs: #group_by