Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Ruby csv column sort

Posted on 2008-10-02
12
Medium Priority
?
1,478 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
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 1000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Integration Management Part 2
Suggested Courses
Course of the Month21 days, 1 hour left to enroll

810 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