We help IT Professionals succeed at work.

Ruby csv column sort

philsivyer
philsivyer asked
on
2,432 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
Comment
Watch Question

Gertone (Geert Bormans)Information Architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
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
Gertone (Geert Bormans)Information Architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
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 :-)

Author

Commented:
Thanks Gertone
Have you used Ruport and if so what do you think of it?

Phil
Gertone (Geert Bormans)Information Architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
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

Author

Commented:
What sort of reports do you normally generate?

Author

Commented:
Where would I put the reverse! so as to sort descending?
Gertone (Geert Bormans)Information Architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
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

Author

Commented:
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

Author

Commented:
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
Information Architect
CERTIFIED EXPERT
Top Expert 2006
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks Gertone
Gertone (Geert Bormans)Information Architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
welcome
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.