aspose file tools*
The moose likes Servlets and the fly likes Comparator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Comparator" Watch "Comparator" New topic
Author

Comparator

Kelly So
Greenhorn

Joined: Mar 27, 2001
Posts: 20
Dear all,
Here is my code :
String sql = "select * from customer";
Vector v = new Vector();
// Retrieve the sql data into a vector
for(int i=0; i<v.size(); i++)
{
custname = v.elementAt(0);
custaddr = v.elementAt(1);
.......
---- Collections.sort(v, v.elementAt(1));
out.println(custname);
out.println(custaddr);
}
I just want to sort the custaddr in my vector,
First I paste (Collections.sort(v)), it works fine.
But after I paste the comparator (v.elementAt(1)),
the error message occur:
No match was found for method :
sort(java.util.Vector, java.lang.Object)
Can anyone help me to clarify my understanding???
In API, there have a method call
sort(List list, Comparator c)
Is vector is a list???
Or maybe I can only use method
sort(Vector) ?
Sorry about this silly question!!!

Thanks in advance !
Kelly
Syed Khan
Greenhorn

Joined: Aug 06, 2001
Posts: 2
Hello,
The Comparator it asks for is a class that implements the java.util.Comparator interface. This is an interface that allows two objects to be compared.
I guess that your name and address are both Strings. These are deemed to have a "natural" ordering - ie, alphabetical - so there's no need to provide a Comparator. If you had elements in your Vector which had no natural ordering (e.g. a Person object) then you would need to provide a Comparator to tell the Collections class how to sort them. This might be by comparing birth dates, names, or some other attribute of the Person.
And yes, a Vector is a List.
I hope this is of some help.
Syed
DAYANAND BURAMSHETTY
Ranch Hand

Joined: Aug 06, 2001
Posts: 34
U can get the all the records in ASCENDING ORDER FROM DATABASE
NO NEED TO DO IN ANYTHING IN THIS FUNCTION
String sortedBy="address";// fieldNames (Like..CustomerName //,CustomerAddress..
String order="ASC"; // ASC or DESC
String sql = "select * from customer ORDER BY "+ sortedBy + " "+order ;";
Vector v = new Vector();
// Retrieve the sql data into a vector
for(int i=0; i<v.size(); i++)
{
custname = v.elementAt(0);
custaddr = v.elementAt(1);
.......
out.println(custname);
out.println(custaddr);
}

Dil se....,<BR>Dayanand<BR>0065-8839071(off)<BR>0065-7547034(Res0
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

To answer your question, a Comparator is any class that implements the java.util.Comparator interface.
Collections.sort() expects two arguements, the first is a java.util.List of things to sort, the second is a description of how to compare the items in the list. In effect you are saying "here is a list of things and here is how to sort them"
When you try to call Collections.sort you are not passing a java.util.Comparator so the parser gets confused and throws you an error.
DAYANAND BURAMSHETTY is correct (but loud) in saying you can do the ordering at the database end but that won't teach you about Comparators.
You need to create a class that implements Comparator like this:
public class MyComparator implements java.util.Comparator
If you have a look at the javadoc for java.util.Comparator, you have to define two methods for it to act as a Comparator:
int compare(Object o1, Object o2)
and
boolean equals(Object obj)
create an implementation of the compare method so that it creates an absolute odrdering of your objects then pass an instance of youre Comparator to the sort method...
Collections.sort(v, new MyComparator());
um, verbose but helpful I hope...
Dave.
ps I think I'm coming down with a cold cos I'm definitely rambling
Kelly So
Greenhorn

Joined: Mar 27, 2001
Posts: 20
Originally posted by DAYANAND BURAMSHETTY:
U can get the all the records in ASCENDING ORDER FROM DATABASE
NO NEED TO DO IN ANYTHING IN THIS FUNCTION

Thanks for Dayanand Buramshetty.....
Your idea is absolutely right, but my sql statement is only a very very simple one. In fact, in my sql statement, it will be combine 4 tables , 3 outer joins and one sub queries......
So I cannot use your ordering.........
Thanks again for all paying attention to my post.

Kelly
 
Consider Paul's rocket mass heater.
 
subject: Comparator