5

I need to be able to rearrange a group of people from their overall fitness score. I am able to do a compareTO method with ints but as soon as its with doubles I get errors flying up everywhere.

public double compareTo(FitnessScore o){
    return (this.overall-o.overall);
}

It needs to be a double because there are scores like:

68.5 68.4 60.1 60.3

So casting them to an int would make it redundant. When I did have it as an int to see if it would work. I tried the following. (subjects being the array that I initialized to hold all the different instances of people)

Arrays.sort(subjects);

I get the following error:

java.lang.NullPointerExceptione

Any suggestions?

Softey
  • 1,451
  • 3
  • 21
  • 42

6 Answers6

14

Your compareTo must alaways return an int value.

-1 for placing it before in Collection
0 indicates same value already exists in Collection
+1 for placing it afterwards in Collection

If overall is of type Double, use this:

public int compareTo(FitnessScore o){
    return this.overall.compareTo(o.overall));
}

If overall is of type double, use this:

public int compareTo(FitnessScore o){
    if(this.overall<o.overall)
          return -1;
    else if(o.overall<this.overall)
          return 1;
    return 0;
}

And for your NullPointerException, check that your subjects is not null.

Aditya
  • 1,334
  • 1
  • 12
  • 23
  • But your method returns a double. And not, it's not - 1 or +1. It's negative, zero, or positive. – JB Nizet Mar 14 '14 at 12:37
  • @JBNizet Thanks for poiting out, forgot to correct it – Aditya Mar 14 '14 at 12:38
  • @Aditya I'm still not sure about the NullPointerException. I have put in a try and catch that just ignores and then processes the information anyway. All my numbers are there so is there a way of combating it or is something that I should just leave? – Softey Mar 14 '14 at 12:46
  • @Softey Please post some more code and also show your error log. – Aditya Mar 14 '14 at 12:48
  • @Aditya I've sorted it. Rogue null made it's way in. My old code works now. Thank you for the answer anyway – Softey Mar 14 '14 at 12:50
7

You are breaking the signature of compareTo method. It should look like below,

public int compareTo(FitnessScore o){
    return Double.compare(this.overall-o.overall);
}
1

You need to implement the Comparator interface. Dont worry about your values. Even if the compare method is returning an int. This int is not one of your values.

Also check this out. Comparator with double type

Community
  • 1
  • 1
GreenOnBlack
  • 68
  • 1
  • 8
0

Your compareTo() method receives a FitnessScore object. However, it does not test for null case.

When o is null, your method will throw a NullPointerException.

Mauren
  • 1,955
  • 2
  • 18
  • 28
0

You can try this.

Double obj1 = this.overall;
Double obj2 = o.overall;

int retval =  obj1.compareTo(obj2);

//then do your normal if else block.

Arjit
  • 3,290
  • 1
  • 17
  • 18
0

You should implement a Comparator interface:

class OFSComparator implements Comparator<Member> {
   public int compare(Member m1, Member m2) {
      return (m1.ofs.compareTo(m2.ofs));
   }
}

Each Member should have a field for overall fitness score

Double ofs;

And if you later want to sort members based on their overall fitness score, you can use Arrays.sort:

Arrays.sort(members, new OFSComparator());
Boris
  • 22,667
  • 16
  • 50
  • 71