tags:

views:

178

answers:

5

I'm trying to establish a new sort criteria, in this case by name. I'm facing an error when I call the sort method...

this is a separated class (SortByName) in package "package":

-----------------------CLASS SortByName---------------------------

package package;

import java.util.*;

public abstract class SortByName implements Comparator{

public int compareTo(Object o1, Object o2){
    String n1 = ((Ficha)o1).getName();
    String n2 = ((Ficha)o2).getName();
    return n1.compareTo(n2);

}

and then inside an ActionPerformed event I have this:

----------------IN THE ACTION EVENT BUTTON----------------------------

Collections.sort( list , new SortByName() );

"package.SortByName is abstract,> cannot be instantiated"

I tried changing the "abstract" type in the class definition (SortByName) , but it complies about not overriding the compareTo() method.

thanks for reading.

A: 

You need to remove "abstract" and implement the equals method as well.

Code should look like:

package package;

import java.util.*;

public class SortByName implements Comparator{

public int compare(Object o1, Object o2){
    String n1 = ((Ficha)o1).getName();
    String n2 = ((Ficha)o2).getName();
    return n1.compareTo(n2);
}

public boolean equals(Object o1) {
 // code which compares the current comparator (!) with the object given
}

Note that you might need to use specialized comparators if compiling against a version of java which supports generics. Not sure if this is correct but I think you can do something like:

public class SortByName implements Comparator <Ficha> {
    public int compare(Ficha o1, Ficha o2){
       // comparing code here
    }
}

But again, not entirely sure on that part, you need to test it a bit.

If you check out the Comparator API that will explain how you must implement equals.

Also note that the Comparator interface defines compare, not compareTo.

laura
Why does a comparator need equals ...?
meriton
-1, wrong method name here as well - compareTo instead of compare
Bozho
Yes I just noticed and edited appropriately. I don't think it justifies a -1 though
laura
Well, this was the core of the question. And it was also because implementing 'equals' is not a 'must'. But I withdraw the -1 now.
Bozho
-1, equals is not needed, as Collections.sort does not require a comparator consistent with equals. Moreover, a comparator is consistent iff the equals method *of the objects it compares* returns true whenever compare returns 0.
meriton
That is, you are overriding the wrong equals method to make it consistent.
meriton
P.S. I'm not allowed, although you edited the answer..
Bozho
@bozho, at least he solved his problem.
laura
@meriton, if the interface requires it, it must be there; the fact that you can use the default comparator in class `Object` instead of implementing it yourself does not change the fact that it is required by the interface
laura
yes.. although this would be one of many, since he obviously isn't too keen on reading documentation.
Bozho
@Laura: The interface does not require that a comparator is consistent with equals. And you are missing the point: If consistency is required, he'd have to override `Ficha.equals`, not `SortedByName.equals`
meriton
+6  A: 

The method you have to implement is called compare, not compareTo.

Abstract classes cannot be instantiated.

Also, Comparator is generic, so you'd better do the following:

public class SortByName implements Comparator<Ficha>{

    public int compareTo(Ficha f1, Ficha f2){
        String n1 = f1.getName();
        String n2 = f2.getName();
        return n1.compareTo(n2);
    }
}
Bozho
+3  A: 

Comparator needs to implement compare(), not compareTo() method.

ZZ Coder
+3  A: 
class SortByName implements Comparator<Ficha>{
@Override
public int compare(Ficha o1, Ficha o2) {
    String n1 = o1.getName();
    String n2 = o2.getName();
    return n1.compareTo(n2);
}

}

Luno
+1 for the proper post-Java 1.5 way of implementing that interface.
meriton
A: 

Laura you were right I had to add the equals method.( u missed the return value) also remove "abstract" at class definition. and change compareTo() with compare().

thanks a lot Laura, PROBLEM SOLVED !!!!

HoNgOuRu
Of it was solved then mark as correct!
Oskar Kjellin
how do I do that ?
HoNgOuRu