• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

sort problem

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at the Javadocs for the Collections class. Look at all the methods. See any that might be useful?
 
Raghav K Aggarwala
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic