Reputation: 4840
I am learning Java and want to sort an array containing objects. To do that I want to use Comparable. I consistently run into a compile error that seems to point out that I either did not implement compareTo (I did) or have something wrong with Comparator (which I do not want to use by now). I have isolated the problem in a small program that I list below. Any help is greatly appreciated because I have spent hours searching on the web.
I have commented out some code of main because what I really want to do is to create an array with interface objects and sort it using Comparable. I am not sure whether that is possible, so I am now testing sorting classes.
package comp_test;
import java.util.ArrayList;
import java.util.List;
public class program {
public static void main(String[] args)
{
List<root> container = new ArrayList<root> ();
container.add (new A ("Een"));
// container.add (new AA ("twee"));
container.add (new B ("drie"));
// container.add (new AA ("vier"));
container.add (new A ("en Vijf"));
for (root r: container)
{
r.show ();
} // for
List.sort (container); // <=== error
// The method sort(Comparator) in the type List is not
// applicable for the arguments (List<root>
}
/*
public static void main(String[] args)
{
List<Itf> container = new ArrayList<Itf> ();
container.add (new A ("Een"));
container.add (new AA ("twee"));
container.add (new B ("drie"));
container.add (new AA ("vier"));
container.add (new A ("en Vijf"));
for (Itf i: container)
{
i.show ();
} // for
container.sort ();
}
*/
}
--------------
package comp_test;
public interface Itf
{
public void show ();
}
---------------
package comp_test;
abstract public class root implements Itf, Comparable<root>
{
abstract public void show ();
abstract public String getID ();
@Override
public int compareTo (root other)
{
return this.getID ().compareTo (other.getID ());
} /*** compareTo ***/
}
------------------
package comp_test;
public class A extends root
{
private String ID;
public A (String id)
{
this.ID = id;
} /*** A ***/
public String getID ()
{
return ID;
} /*** getID ***/
@Override
public void show ()
{
System.out.println ("A (" + getID () + "): " + super.toString ());
} /*** show ***/
}
------------------
package comp_test;
public class B extends root
{
private String Name;
public B (String id)
{
this.Name = id;
} /*** B ***/
@Override
public String getID ()
{
return Name;
} /*** getID ***/
@Override
public void show ()
{
System.out.println ("B (" + Name + "): " + super.toString ());
} /*** show ***/
}
Upvotes: 1
Views: 174
Reputation: 11621
You tried to call sort like this:
List.sort(container);
Whenever you get a compiler error like this, you should look at the Javadoc for the method that caused the error. Looking at the Javadoc for List.sort, we can see that the method is not static, but you are trying to use it as if it was. Since the method is not static, you need to call it on the object you are trying to sort:
container.sort(null);
Reading further about the method, the Javadoc says that if you want to use the contained objects' natural ordering (i.e. their compareTo method) then you should pass null, instead of a comparator (as I've done above).
By the way, you can also call Collections.sort(container);
but that is just a wrapper and all it does is call container.sort(null)
.
Upvotes: 2
Reputation: 60104
instead of
List.sort (container);
you should use
Collections.sort (container);
or
container.sort(new Comparator<root>() {
@Override
public int compare(root o1, root o2) {
return o1.compareTo(o2);
}
});
or (using lambda):
container.sort((o1, o2) -> o1.compareTo(o2));
or (using lambda, case 2):
container.sort(root::compareTo);
Upvotes: 1
Reputation: 3577
You should use this :
container.sort()
It will use the compareTo method of your root elements that populate the List
PS : root-->Root (Object naming start will capital letter)
Upvotes: 1