Sort lines alpahetically

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

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

On Jan 16, 2008 9:04 AM, Junkone [email protected] 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 :slight_smile:

kind regards -botp

On Wed, 16 Jan 2008, Junkone wrote:

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

From the commandline :-
sort fileName

or if you must do it from ruby
sort #{filename}

or if you really don’t want to break out to another utility

IO.read( fileName).split.sort

John C. Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : [email protected]
New Zealand

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs