Arrays.sort
class A {
int x;
A(int x) {
this.x = x;
}
}
public class Main {
public static void main(String[] args) {
A[] a = new A[4];
a[0] = new A(3);
a[1] = new A(4);
a[2] = new A(1);
a[3] = new A(2);
Arrays.sort(a, new Comparator<A>() {
@Override
public int compare(A o1, A o2) {
return o1.x - o2.x;
}
});
System.out.println(a[0].x);
System.out.println(a[1].x);
System.out.println(a[2].x);
System.out.println(a[3].x);
}
}
class A{
int x;
A(int x) {
this.x = x;
}
}
public class Main {
public static void main(String[] args) {
List<A> a = new ArrayList<>();
a.add(new A(3));
a.add(new A(4));
a.add(new A(1));
a.add(new A(2));
Collections.sort(a, new Comparator<A>() {
@Override
public int compare(A o1, A o2) {
return o1.x - o2.x;
}
});
System.out.println(a.get(0).x);
System.out.println(a.get(1).x);
System.out.println(a.get(2).x);
System.out.println(a.get(3).x);
}
}