通过对象字段对对象进行排序。如何制作比较器? Java/处理

发布于 2025-01-20 13:27:11 字数 1852 浏览 3 评论 0原文

我有一个名为 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

如何在 Java 中对对象数组进行排序?

谢谢!

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦毁影碎の 2025-01-27 13:27:11

最简单的方法是使用比较器。假设您的 Creature 类中有一个 getDistance 方法

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文