I have done half of the work. The following program take names from a text file and sort it based on first name. The question in JavaRanch Cattle Drive also asks for sorting the ArrayList for last name. Can anyone suggest how that can be achieved?
Here is the class file I've written so far that sorts first name.
When I call readLine() from BufferedReader, it returns a string containing "First Last".
When using Collections.sort(), it sorts only on the basis of first name Last name. How can make it sort based on Last name First name (which is in the same string separated from First name by " ".
Can it be achieved without creating another object of ArrayList?
I could not find any method in Collections class that can sort based on string after " ".
--------------- The solution would be for following JavaRanch Cattle Drive Question.
Load an ArrayList with Strings from a text file of names. Show the names sorted in order of first name and then by last name. Use com.javaranch.common.TextFileIn to read the names. Use the Collections class for sorting. Do not create a second list with the names in a different order. Do not modify the ArrayList or its contents except by using the Collections class. Your class will be called SortNames.
Thank you for your help.
author and iconoclast