通过对象字段对对象进行排序。如何制作比较器? Java/处理
我有一个名为 Creature 的类和一个名为 Creature 的 Creature 对象数组。 Creature 类有一个称为距离的整数字段。我想按阵列生物各自的距离场对它们进行排序。我尝试了几种不同的方法,但还没有走得太远。有人可以向我解释这是如何工作的,以便我能弄清楚如何做到这一点吗?
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;
}
}
*/
}
我有时会收到静态错误,有时则不会。我是否正确设置了可比性?如果是这样我该如何使用它?我不在乎如何对数组进行排序,但最简单的方法会很好。顺便说一下,我正在使用处理。
这些是我迄今为止尝试使用的一些来源:
<一个href="https://stackoverflow.com/questions/12449766/java-sorting-sort-an-array-of-objects-by-property-object-not-allowed-to-use-co">Java 排序:排序按属性排列的对象数组,对象不允许使用Comparable
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单的方法是使用比较器。假设您的
Creature
类中有一个getDistance
方法The easiest way would be to use a Comparator. Assuming there is a
getDistance
method in yourCreature
class