# Sort lines alpahetically

**URL:** <https://www.ruby-forum.com/t/sort-lines-alpahetically/126176>\
**Category:** Ruby\
**Created:** [January 16, 2008, 2:05am UTC](https://www.ruby-forum.com/t/sort-lines-alpahetically/126176 "2008-01-16T02:05:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Junkone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Junkone](https://www.ruby-forum.com/u/Junkone)\
**Post date:** [January 16, 2008, 2:05am UTC](https://www.ruby-forum.com/t/sort-lines-alpahetically/126176/1 "2008-01-16T02:05:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Junkone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Junkone](https://www.ruby-forum.com/u/Junkone)\
**Post date:** [January 16, 2008, 3:14am UTC](https://www.ruby-forum.com/t/sort-lines-alpahetically/126176/2 "2008-01-16T03:14:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Junkone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Junkone](https://www.ruby-forum.com/u/Junkone)\
**Post date:** [January 16, 2008, 3:27am UTC](https://www.ruby-forum.com/t/sort-lines-alpahetically/126176/3 "2008-01-16T03:27:11Z")

</div>

On Jan 16, 2008 9:04 AM, Junkone [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![Junkone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Junkone](https://www.ruby-forum.com/u/Junkone)\
**Post date:** [January 16, 2008, 8:03pm UTC](https://www.ruby-forum.com/t/sort-lines-alpahetically/126176/4 "2008-01-16T20:03:23Z")

</div>

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 : removed\_email\_address@domain.invalid  
New Zealand
