2

I have a class called Creature and an array of Creature objects called creatures. The class Creature has an integer field called distance. I would like to sort the array creatures by their individual distance fields. I've tried several different ways but haven't gotten very far. Can someone explain to me how this works so I can figure out how to do it?

Creature [] creatures;
int [] distancearray;                      //I made a seperate array to try and order the 
                                           //creatures array based on this array

distancearray = new int [numcreatures];
for (int i = 0; i < numcreatures; i++) {
  distancearray[i] = creatures[i].distance;
}
distancearray = sort(distancearray);      //sorting this array was easy but idk how to 
                                          //sort creatures based on it
...

class Creature /*implements Comparable*/{//First attempt at Comparable
...
/*
  int compareTo(Object otherObject){                //First try with Comparable
    Creature otherCreature = (Creature)otherObject;
    if (distance < otherCreature.distance) {
      return -1;
    }
    if (distance > otherCreature.distance) {
      return 1;
    } else {
      return 0;
    }
  }

  int compare(Creature C2){                         //Second try with Comparable
    if (distance < C2.distance) {
      return -1;
    }
    if (distance > C2.distance) {
      return 1;
    } else {
      return 0;
    }
  }
*/
}

I sometimes get a static error, sometimes not. Am I setting the comparable up correctly? If so how do I then use it? I don't care how I get the array sorted but the easiest way would be nice. I am using Processing by the way.

These are some of the sources I've tried to use so far:

Making a generic comparator class

Java Sorting: sort an array of objects by property, object not allowed to use Comparable

How to sort an array of objects in Java?

Thanks!

1 Answers1

2

The easiest way would be to use a Comparator. Assuming there is a getDistance method in your Creature class

import java.util.Arrays;
import java.util.Comparator;

//may be more of your Processing code

Creature [] creatures = //your array;

void setup() {
  //sort your array here or in whatever method you want
  Arrays.sort(creatures, Comparator.comparingInt(Creature::getDistance));
}

class Creature {
    // your class code
}

//may be more of your Processing code
Eritrean
  • 15,851
  • 3
  • 22
  • 28
  • Thanks for the response! I'm confused about your syntax tho. Do I have to swap out "Arrays" in your code for something? Also, do I need to make a Comparator? – William Chandler Apr 11 '22 at 18:40
  • 1
    @WilliamChandler Just import `import java.util.Arrays; import java.util.Comparator;` and use it as above. You don't need to modify it, no need for `Comparable` or any other methods. Just the one-liner above. – Eritrean Apr 11 '22 at 18:51
  • 1
    @WilliamChandler Please see my edit. Hope it is clear now. – Eritrean Apr 11 '22 at 19:06
  • "comparingInt() expects parameters like: "comparingInt(ToIntFunction super T>)" I'm assuming the problem is with my getDistance method. getDistance just sets the current distance does getDistance need to output something? An int sort key? – William Chandler Apr 11 '22 at 19:08
  • Do I need to use any of the code in my question? I will edit it to show what I currently have commented out. – William Chandler Apr 11 '22 at 19:12
  • @WilliamChandler " getDistance just sets the current distance" That's strange. By convention `getDistance()` *gets* the distance, and in the Eritrean's example, it's assumed it returns `int`. If you need to set the distance, name that accessor method `setDistance()` – erickson Apr 11 '22 at 19:31
  • The method I'm talking about is actually called checkdistance(). I didn't realize there was a standard nomenclature. – William Chandler Apr 11 '22 at 19:37
  • 1
    It worked though once I made those changes! @Eritrean Thank you so much for helping me! – William Chandler Apr 11 '22 at 19:42
  • @erickson Thanks for teaching me some nomenclature! – William Chandler Apr 11 '22 at 19:42