In another tutorial, we saw how we could get a List of the lines of a File very easily using the readLines() method of the FilesUtils class of the ApacheSW Commons IOS library. We can very easily alphabetize the List of lines with a call to Collections.sort(), since a List is a Collection. The ReadListOfLinesFromFileAndAlphabetize class demonstrates this.

test.txt

this is
a test
of commons io

Executing ReadListOfLinesFromFileAndAlphabetize with test.txt generates the following console output:

line:a test
line:of commons io
line:this is

Note that Collections.sort() will treat uppercase and lowercase characters as not being equal when it performs a sort. To ignore case (and treat uppercase/lowercase the same), you can create a Comparator for strings and call toLowerCase() or toUppercase() when you compare them to 'equalize' the cases.