1

I am working on existing code. In the code a list of objects is being created then Collections.sort and Collections.reverse are used on this list.

Here is a small example of the class they wanted to be comparable:

public class Student implements Comparable<Student> {
    public int compareTo(Student s) {
        //code comparing the instance's score to parameter score
    }
}

The list is of type Student and the output is the objects in descending order based on score. Is it good or bad practice to have an instance know about another instance of itself?

My question with using the Comparable, is there a better way to accomplish what they were trying to achieve?

EDIT: Still getting used to explaining my questions to other people. I am still a student, so thank you for that tip.

eparham7861
  • 205
  • 3
  • 12
  • 2
    Provide a custom `Comparator` when calling `Collections#sort(List, Comparator extends YourClass>);` – Luiggi Mendoza Mar 02 '15 at 14:25
  • When to use Comparable and Comparator: http://stackoverflow.com/questions/2266827/when-to-use-comparable-and-comparator?rq=1 – Luciano Mar 02 '15 at 14:35
  • Instead of creating a verbose description of this simple case, just post a small code snippet to describe your problem. – SME_Dev Mar 02 '15 at 14:36
  • That would be the best place to put it, if you want to compare yourself to someone else, who is better to do it than you? – Viktor Mellgren Mar 02 '15 at 14:52
  • Luiggi-I will need to figure out how to use Comparator, but I will look into using that. Vixen- I understand what you are saying. If I compare myself to someone else there could be an internal or external comparison. The internal being this example code. The external being where each person, or object, produces their data on a medium where the comparison would be administered, which for this case would be some main. – eparham7861 Mar 02 '15 at 15:02

2 Answers2

0

The other possible way is to implement Comparator and provide it as a parameter to Collection.sort method. Similar to that:

Comparator<Integer> comparator = new Comparator<Integer>() {
   @Override
   public int compare(Integer o1, Integer o2) {
      return o1 - o2;
   }
};
Collections.sort(yourList, comparator); 

Where yourList is List<Integer> yourList = new ArrayList<>();

maxormo
  • 599
  • 1
  • 4
  • 10
0

The way that you describe the existing code reflects best practice for this situation. This is how compareTo is supposed to work. You could provide an external implementation of a Comparator, but there is no good reason to do so here.

GreyBeardedGeek
  • 29,460
  • 2
  • 47
  • 67
  • The only issue that I am feeling towards using Comparable is that they are using it to order the list of object based on the score. Couldn't I just do that on my own in a method without using Comparable? – eparham7861 Mar 02 '15 at 15:06
  • Yes, you can implement Comparator. Typically, you would have Student implement the Comparable interface, and implement whatever you consider to be Student's "natural" ordering. You would implement Comparator for other, secondary orderings. – GreyBeardedGeek Mar 02 '15 at 20:02