Solved

Ruby csv column sort

Posted on 2008-10-02
12
1,315 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

688 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