Solved

Ruby csv column sort

Posted on 2008-10-02
12
1,186 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
trying to get text between certain words 6 215
Ruby Rails form 4 261
git - conflict 5 219
Ruby error "undefined method" 6 77
In Ruby, Call or invoke a API DLL library is easily via Win32API class, win32-api gem or other gems. For general DLL API call, there are quite a few references, some good tips list below: http://www.rubytips.org/2008/05/13/accessing-windows-api-fro…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

948 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

23 Experts available now in Live!

Get 1:1 Help Now