我有一个名为apple的类,它包含3个值,分别为int x,int y和int weight.然后我创建了一个苹果类型对象的数组.现在我想根据权重对对象数组进行排序,这意味着具有最低权重的苹果对象应该是第一个,依此类推.
我知道有很多方法可以通过使用Arrays.sort等或比较器来实现这一点.
我想知道在Java中这种方法的最快方法是什么?可能有一个案例,我有500,000个对象,所以我想知道我应该使用哪种,更重要的是哪种方法会给我最好的方法.我甚至用Hoare分区编写了自己的快速排序.
Apple类的代码
public class Apple {
public int x;
public int y;
public int weight;
public Apple(int a, int b, int w) {
x = a;
y = b;
weight = w;
}
}
主类代码
public class main {
static Apple[] appleArray;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
int totalApples = sc.nextInt();
appleArray = new Edge[totalApples];
int x = 10;
int y = 20;
int w = 30;
for (int i = 0; i < size; i++) {
appleArray[i] = new Apple(x, y, w);
x++;
y++;
w++;
}
//Now i want to sort array of apple objects based on weight
}
}