Solved

Ruby csv column sort

Posted on 2008-10-02
12
1,301 Views
Last Modified: 2012-08-13
Hello
Want to know how to sort by column asc or desc
Script is .......
require 'csv'  
  people = CSV.read("c:/foo.txt")
  joe = people.find_all {|person| person[0] =~ /Joe/}
 
#when writing back to file I want to sort by column 2 ascending (file has 3 columns)
  CSV.open("c:/fooreturn.txt","w") do |csv|
    joe.each do |person|
      csv <<person
    end
  end
0
Comment
Question by:philsivyer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
12 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 22626707
I suspect that what you need is the sort_by method,
like this
 joe = people.find_all {|person| person[0] =~ /Joe/}
 joe = joe.sort_by {|j| j[0]}

I assume that joe is an enumerable of some kind
likely it is an array of arrays

If this doesn't work, can you then post an example of foo.txt, so I can test,
thanks

Geert
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 22626794
yep, this works,
of course you will need this
  joe = joe.sort_by {|j| j[1]}
(so 1 instead of zero)
sorting on 'Joe' is not exciting enough :-)
0
 

Author Comment

by:philsivyer
ID: 22628106
Thanks Gertone
Have you used Ruport and if so what do you think of it?

Phil
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 60

Expert Comment

by:Geert Bormans
ID: 22628123
never used it, but I can a look tomorrow if you want.
I usually handle reporting with my own coding, but it could be wise to use ruport instead
0
 

Author Comment

by:philsivyer
ID: 22628259
What sort of reports do you normally generate?
0
 

Author Comment

by:philsivyer
ID: 22628334
Where would I put the reverse! so as to sort descending?
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 22628515
joe = joe.sort_by {|j| j[1]}.reverse!

I usually create reports in html (hacked together with ruby scripting)
so they can be viewed with a browser
often in different files, with links etc.
usually pretty  specific, hence my hacking mood instead of using something like ruport
0
 

Author Comment

by:philsivyer
ID: 22632803
Gertone
How do you graph - do you use something like "scruffy"  - I deliver reports using Crystal Enterprise (Business Objects) and by developing sql scripts (stored procedures) which feeds data to Crystal.
Just interested in looking at other avenues such as your approach - any examples.
Phil
0
 

Author Comment

by:philsivyer
ID: 22634303
Gertone
Final question:being playing around trying to return column 2 only to output file - without success.
How can you return selective columns - say in this case columns 1 & 3

Phil
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 250 total points
ID: 22643795
graphs:
various techniques used
- the svg library
- creating XML, than generating SVG using XSLT

I have not used scruffy

filtering out the second column... you can use a collect statement
  joe = people.find_all {|person| person[0] =~ /Joe/}
  joe = joe.sort_by {|j| j[1]}.reverse!
  joe.collect!{|arr| [arr[0] , arr[2]]}
0
 

Author Comment

by:philsivyer
ID: 22645577
Thanks Gertone
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 22645582
welcome
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Choosing a Server-Side Scripting Language - Criterion 4 6 452
how to write into a file 1 220
subversion post-commit hook issue 4 570
How chef gets upgraded automatically? 1 596
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…
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

734 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