배열 정렬
오름차순
int[] array = new int[]{42, 25, 60, 73, 103, 167};
Arrays.sort(array);
ArrayList<Integer> arrayList = new ArrayList<>();
arrayList.add(5);
arrayList.add(4);
arrayList.add(3);
arrayList.add(2);
arrayList.add(1);
Collections.sort(arrayList);
arrayList.sort(Comparator.naturalOrder());
- int 배열:
Arrays.sort(array)
- Integer 배열
Collections.sort(arrayList)
arrayList.sort(Comparator.naturalOrder())
내림차순
int[] array = new int[]{42, 25, 60, 73, 103, 167};
Integer[] newArray = Arrays.stream(array).boxed().toArray(Integer[]::new);
Arrays.sort(newArray, Collections.reverseOrder());
ArrayList<Integer> arrayList = new ArrayList<>();
Collections.sort(arrayList, Collections.reverseOrder());
arrayList.sort(Comparator.reverseOrder());
- int 배열:
Integer[] newArray = Arrays.stream(array).boxed().toArray(Integer[]::new); Arrays.sort(newArray, Collections.reverseOrder());
- Integer 배열
Collections.sort(arrayList, Collections.reverseOrder())
arrayList.sort(Comparator.reverseOrder())
[참고]
https://coding-factory.tistory.com/549