int, char와 같은 primitive type 배열 정렬 지원 (예외로 String도 지원)
해당 함수는 dual pivot quicksort 정렬 알고리즘을 활용. (primitive type일때)
일반 quickSort는 O(n^2) 복잡도를 지님
→ 이러한 퀵소트 속도를 보안하여 만든 것이 dual pivot quicksort.
dual pivot quick sort = 삽입 정렬 + 퀵 정렬
사용법
**오름차순**
import java.util.Arrays;
public class Sort {
public static void main(String[] args) {
int[] array = {58, 32, 64, 12, 15, 99};
Arrays.sort(array);
}
}
**일부 오름차순**
import java.util.Arrays;
public class Sort {
public static void main(String[] args) {
Integer[] array = {58, 32, 64, 12, 15, 99};
Arrays.sort(array, 1, 4);
}
}
int[] arr = {4, 3, 6, 1, 2};
Integer[] arr2 = Arrays.stream(arr).boxed().toArray(Integer[]::new); //방법1
Arrays.sort(arr2, Collections.reverseOrder());
arr = Arrays.stream(arr).boxed().sorted(Collections.reverseOrder()).mapToInt(Integer::intValue).toArray(); //방법2
**내림차순**
import java.util.Arrays;
import java.util.Collections;
public class Sort {
public static void main(String[] args) {
Integer[] array = {58, 32, 64, 12, 15, 99};
Arrays.sort(array, Collections.reverseOrder());
}
}
**일부 내림차순**
import java.util.Arrays;
import java.util.Collections;
public class Sort {
public static void main(String[] args) {
Intege array = {58, 32, 64, 12, 15, 99};
Arrays.sort(array, 1, 4, Collections.reverseOrder());
}
}
List<Student> sList = new ArrayList<>();
Comparator<Student> cp = new Comparator<>() {
@Override
public int compare(Student s1, Student s2) {
return s2.age - s1.age;
}
};
Collections.sort(sList, cp);
Arrays.sort(months,
(String a, String b) -> a.length() - b.length());
→ 아래 코드: 더 깔끔한 방식
Arrays.sort(months, Comparator.comparingInt(String::length));
List<Player> sortedPlayers = players.stream()
.sorted((a, b) -> b.getScore() - a.getScore())
.collect(Collectors.toList());