I have a class called apple that contains 3 values ββlike int x , int y and int weight . Then I created an array of objects like Apple. Now I want to sort the array of objects by weight, meaning that the object with the smallest weight should be the first, etc.
I know that there are many ways to achieve this using Array.sort, etc. or comparators.
I was wondering what is the fastest way to make this look in Java? There may be times when I have 500,000 objects, so I want to know what type I should use, more importantly, which approach will give me the best approach. I even wrote my own quick view with the Hoare section.
Code for Apple Class
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; } }
Code for the main class
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++; }
source share