Reputation: 279
I have only class files and no source code in java, Now I have to sort them, what shall I use Comparator or Comparable?
Appreciate your help.
Upvotes: 4
Views: 279
Reputation: 3938
Go for Comparator.
(extracted from http://www.digizol.com/2008/07/java-sorting-comparator-vs-comparable.html)
Comparable
A comparable object is capable of comparing itself with another object. The class itself must implement the java.lang.Comparable interface in order to be able to compare its instances.
Comparator
A comparator object is capable of comparing two different objects. The class is not comparing its instances, but some other class’s instances. This comparator class must implement the java.util.Comparator interface.
Your current class is already implemented. Even if it already has a Comparable implementation, you are not aware how that comparison is implemented; hence you cannot predict your sorting. When you are not modifying the source of the class which you are trying to sort, the only option is to go with Comparator.
Upvotes: 0
Reputation: 509
If possible you can extend the class and write a new class, which implements Comparable so that you can implement compareTo(). In other case you can use, Comparator as given ,
class MyComparator implements Comparator<MyClass>{
@Override
public int compare(MyClass o1, MyClass o2) {
return o1.getType().compareTo(o2.getType());
}
}
Using comparator provides you with more customization needed.
Upvotes: 0
Reputation: 7899
If you don't have access to source file then use Comparator
to sort them.
class MyComparator implements Comparator<MyClass>{
@Override
public int compare(MyClass o1, MyClass o2) {
return o1.getType().compareTo(o2.getType());
}
}
// where getType is the getter method of the field on which you want to sort.
Upvotes: 0
Reputation: 14096
Comparable
is an interface implemented by classes that know how to compare themselfs with another instance of that class.
Comparator
is an interface for comparing two instances of a different class.
If you have a Person
class, e.g.
public class Person implements Comparable<Person> {
private final String firstName;
private final String lastName;
...
public int compareTo(Person that) {
int rv = lastName.compareTo(that.lastName);
if (rv == 0)
rv = firstName.compareTo(that.firstName);
return rv;
}
}
That is a class that has a natural sort order of by last name and then by first name, i.e. Stephen Jones comes before John Smith.
If you then wanted to sort those objects by first name and then last name, such that John Smith comes before Stephen Jones you would use a Comparator
public class PersonComparator implements Comparator<Person> {
public int compare(Person p1, Person p2) {
int rv = p1.getFirstName().compareTo(p2.getFirstName());
if (rv == 0)
rv = p1.getLastName().compareTo(p2.getLastName());
return rv;
}
}
Which you use depends on what control you have over the classes and the instances.
For example, if you have a class that does not implement Comparable
, but you can subclass it to implement the interface and you are sure that you will be the only person creating instances, then you can use Comparable
.
In general, however, Comparator
is the more flexible.
Upvotes: 4