wood burning stoves 2.0*
The moose likes Java in General and the fly likes sort problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "sort problem" Watch "sort problem" New topic
Author

sort problem

Raghav K Aggarwala
Greenhorn

Joined: Jun 04, 2006
Posts: 9
Hi all...

Trying to write a program for JavaRanch Cattle Drive http://www.javaranch.com/drive/3/index.jsp#sort

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.


--------------------------------------------------------------------

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;

import com.javaranch.common.*;

public class SortNames {


private String fileName;
private String temp;
private boolean flag = true;
private TextFileIn file;
private ArrayList list;

public SortNames(String fileName)
{
super();
this.fileName = fileName;
execute();
}
public void execute()
{


try {

file = new TextFileIn(fileName);

}
catch (FileNotFoundException fnfe)
{
System.out.println(fnfe.getMessage());
}



list = new ArrayList();

try {
while(flag)
{

temp = file.readLine();
if(temp==null) {flag=false;break;}
else
list.add(temp);


}
Collections.sort(list);

for(int i =0; i<list.size();i++)
{

System.out.println(list.get(i));
}

}
catch (Exception e)
{
e.printStackTrace();
}

}

public static void main(String[] args) {

new SortNames("c:/Practice/RA/Workspace/TutPrac/textnames.txt");

}

}
--------------------------------------------------------------

- Text file textnames.txt has following names

Adam Baum
Justin Case
Bill Joy
James Gosling
Walter Cronkite
Joe Montana
Abraham Lincoln
Connie Chung


Thank you all for help.

Raghav
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Have a look at the Javadocs for the Collections class. Look at all the methods. See any that might be useful?


[Jess in Action][AskingGoodQuestions]
Raghav K Aggarwala
Greenhorn

Joined: Jun 04, 2006
Posts: 9
Hi Ernest,

Name list has first name and last name like this

First Last

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.

Raghav
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Originally posted by r aggarwal:

I could not find any method in Collections class that can sort based on string after " ".


You didn't look hard enough. I'll make it a little easier for you: look at all the methods named "sort()".
Raghav K Aggarwala
Greenhorn

Joined: Jun 04, 2006
Posts: 9
Hi Ernest:

I have implemented Comparator interface, implemented compare(Object arg0, Object arg1) method with desired functionality, used Collections.sort(list,this) and it works.



Thank you.

Is there any harm in using this versus using a new instance of the same class??



Thanks again,

Raghav
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sort problem