Link to home
Start Free TrialLog in
Avatar of philsivyer
philsivyer

asked on

def - sort_by

Can someone tell me what is wrong with the sort_by? - see "def printfilelinedetails"
Trying to sort by column 3 - but not working.

Regards
Phil
require "csv"  
class CSVtest  
def initialize(filename)
   @filename = filename
   @results = CSV.read(@filename)
end 
def printfilelinedetails
   @results = File.open(@filename)                                        
   @results.each do |line|
   sorted = line.sort_by{|w|w[2]}   
   puts sorted
  end   
end
 
def printReferenceNos
    refnos = @results.find_all do |r|   
      r[1]=="Ref0001"  or r[1]=="Ref0002"
    end  
    puts "#{Time.now}\nFollowing reference numbers were selected:"  
    refnos.each {|m| puts "#{m[1]},#{m[2].downcase}"}  
end  
 
def sumcol3
    puts "the total of Man Hours Lost is: #{ @results.inject(0){|sum, line| sum + line[3].to_f}.to_s}"
    end
    
def selectVectusorSAP
   @results = File.open(@filename)                                        
   @results.each do |line|
     if line =~ /(vectus)|(sap)/i then
       line
   puts line
   end
  end   
end
 
phil = CSVtest.new("C:/RUBY_WORKING_MODELS/csv_master.txt")  
phil.printfilelinedetails
puts "\n"
phil.selectVectusorSAP
puts "\n"
phil = CSVtest.new("C:/RUBY_WORKING_MODELS/csv_master.txt")  
phil.printReferenceNos 
puts "\n"
phil.sumcol3
end

Open in new window

Avatar of cminear
cminear

You want to apply the 'sort_by' method directly to the @results variable.  The returned File instance is enumerable, by itself (which is why 'each' can be used, too).  When you applied the 'each' method, each line is a String, so when you then use 'sort_by', you are trying to sort the line, in-place, which you then print out.  (And because you are accessing an array index, you aren't getting what you want, obviously.)

You also say you want to sort by _column_ 3; the current code (opening the file with File.open) would sort by the 3rd character column, not the 3rd CSV column.  Furthermore, you are essentially overwriting the @results instance variable that was set during initialization, which you may not want to do.

I believe the code snippet below contains a corrected 'printfilelinedetails' method.  Note that the sorting of the results is immediately passed to 'each' to be outputted, and that we "rebuild" the line.  (If you really want the line as it originally is in the file, and hence use the 'File.open', then you need to perform the splitting yourself in the 'sort_by' method call.)  'sort_by' does not alter the contents of the @results variable, meaning @results if used again is still unsorted.
def printfilelinedetails
  #@results = File.open(@filename)                                        
  @results.sort_by {|line|
    line[2]  
    # line[2].to_i  (if you want to sort by number)
    # line[2].to_lower (if you want to sort alphabetically)
  }.each {|line_set|
    puts line_set.join(",")
  }
end

Open in new window

Avatar of philsivyer

ASKER

Hello
Thanks for the response - still strugling with the sort_by logic. Can I try a new approach - assume my file is as follows:
COL_A, COL_B,COL_C
A,          2,          SS
C,          1,           XX
B,          24,         123
what is the code for sorting by Col_b then Col_a then Colc?

Regards


ASKER CERTIFIED SOLUTION
Avatar of cminear
cminear

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
cminear
Thanks for this - works a treat

Regards
Phil
Good work - thanks for prompt reply