1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| import java.util.ArrayList; import java.util.Collections; public class SortArrayList { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<>(); list.add(10); list.add(5); list.add(3); list.add(2); list.add(8);
Collections.sort(list, new MyComparator());
for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } } static class MyComparator implements Comparator<Integer> { @Override public int compare(Integer o1, Integer o2) { return o2 - o1; } } }
|