How to compare dates coming from a CSV file in Ruby

2

Good personal I have a CSV file with some data in it in case this:

Nome; Data; Salario

JOANNA  1981-04-11  7519.07
LUCIMAR 1958-06-10  819.77
PEDRO   1976-05-11  83.43    
JOAO    1989-03-12  867.5    
CAIO    1954-02-13  88.6   
JULIANA 1958-07-15  884.78    
LUCIMAR 1958-02-16  894.7

I wanted to know how do I compare these two dates and know which is the largest and the smallest and also know the names of people who were born on the same date someone can give me a light. I know that when I read the CSV file the data comes in an array so I can not compare the same ones to be able to help thanks.

    
asked by anonymous 04.07.2015 / 05:31

1 answer

2

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

  • 04.08.2015 / 13:17