[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

sort lines alpahetically

Junkone

1/16/2008 1:02:00 AM

What is the fastest way to sort all the lines of a file alphabetically.
2 Answers

botp

1/16/2008 2:13:00 AM

0

On Jan 16, 2008 9:04 AM, Junkone <junkone1@gmail.com> wrote:
> What is the fastest way to sort all the lines of a file alphabetically.

i cannot answer the question regarding "fastest" (to run, right?).

sometimes, my fastest means fastest solution i can come up with, but
not really fastest in speed. besides, things that are fast may not be
so easy to hack up with like eg you want to ignore blank lines and
lines with leading whitespace or special characters.

anyway i usually do it first on os level, like

C:\family\ruby>cat source | sort > sourcesorted

or in ruby, i usually use rio since it seems to take care of memory hogs,

C:\family\ruby>cat test.rb
require 'rubygems'
require 'rio'
rio("sourcesorted") < rio("source").readlines.sort


and there are of course better and cooler and more granular ways...
ymmv. just remember, in ruby, everything is possible in many ways :)

kind regards -botp

Heinrich Piard

1/16/2008 2:16:00 AM

0

Junkone wrote:
> What is the fastest way to sort all the lines of a file alphabetically.

Hi Junkone,

I would suggest to use something like this:

SortedFile = File.readlines('c:\ruby_scripts\test.txt.txt').sort

puts SortedFile



bye
Henry
--
Posted via http://www.ruby-....