Solved

def - sort_by

Posted on 2009-05-06
5
359 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:philsivyer
  • 3
  • 2
5 Comments
 
LVL 12

Expert Comment

by:cminear
Comment Utility
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

0
 

Author Comment

by:philsivyer
Comment Utility
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


0
 
LVL 12

Accepted Solution

by:
cminear earned 500 total points
Comment Utility
Assuming @results is still being initialized with a 'CSV.read', take a look at this code snippet.

Building the array of the items in the order that you want precedence orders how the sorting is done.  Based on your example, I made a point of changing the second column to integers for the comparison, and forcing the first column to be lower-case.  This way '123' does not appear before '2' and 'B' would not appear before 'a'. Because the last column is a mix of strings and numbers, I just leave it as is.

One thing I forgot to mention is that if you are dealing with CSV files in Ruby, to take a look at FasterCSV (http://fastercsv.rubyforge.org/).  As the name implies, it works to be much faster than the standard CSV class.  Of course, if you are using Ruby 1.9.x, FasterCSV has replaced the original CSV class implementation, so you already have it.  (If you are dealing with small files and/or just learning, there is no reason to change anything just yet.  I'm throwing it out there just as an FYI.  Plus, the sorting is done on an array, so parsing a CSV file more quickly doesn't help the sort speed at all.)
def printfilelinedetails

  @results.sort_by {|line|

    [ line[1].to_i, line[0].to_s.to_lower, line[2] ]  

  }.each {|line_set|

    puts line_set.join(",")

  }

end

Open in new window

0
 

Author Comment

by:philsivyer
Comment Utility
cminear
Thanks for this - works a treat

Regards
Phil
0
 

Author Closing Comment

by:philsivyer
Comment Utility
Good work - thanks for prompt reply
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I recently rediscovered rails when I needed a holiday project and decided to build a management dashboard for the company where I work.  With it being a project done in my free time, I could focus my time on learning the basics rather than trying to…
Article by: narshlob
If you've ever programmed in Ruby and have come across either a proc or a lambda, you might have been wondering what the difference is between the two and when you would use one over the other. This article will try to explain the difference between…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now