๋ฐฐ์ด(Array) ์ ๋ ฌ
์ค๋ฆ์ฐจ์ ์ ๋ ฌ
int[] arr = {1, 2, 4, 5, 3};
Arrays.sort(arr);
๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ
int[] arr = {1, 2, 4, 5, 3};
Arrays.sort(arr, Collections.reverseOrder());
์ผ๋ถ๋ง ์ ๋ ฌ
int[] arr = {1, 2, 5, 4, 3}; // 1 2 4 5 3
Arrays.sort(arr, 0, 4); // 1, 2, 4, 5 ์์๋ง ์ ๋ ฌ
๋ฆฌ์คํธ(List) ์ ๋ ฌ
์ค๋ฆ์ฐจ์ ์ ๋ ฌ
ArrayList<String> list = new ArrayList<>(Arrays.asList("c", "a", "b"));
Collections.sort(list); // a b c
ArrayList<String> list = new ArrayList<>(Arrays.asList("c", "a", "b"));
list.sort(Comparator.naturalOrder()); // a b c
๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ
ArrayList<String> list = new ArrayList<>(Arrays.asList("c", "a", "b"));
Collections.sort(list, Collections.reverseOrder()); // c b a
ArrayList<String> list = new ArrayList<>(Arrays.asList("c", "a", "b"));
list.sort(Comparator.reverseOrder()); // a b c
๊ฐ์ฒด ์ ๋ ฌ(Comparator ์ฌ์ฉ)
public class Main {
public static void main(String[] args) {
List<Student> studentList = new ArrayList<Student>(); // ๊ฐ ์ถ๊ฐ ์๋ต
Comparator<Student> comparator = new Comparator<Student>() {
@Override
public int compare(Student a, Student b) {
return a.getAge() - b.getAge(); // ์ค๋ฆ์ฐจ์
}
};
Collections.sort(studentList, comparator);
}
}
public class Student {
private String name;
private int age;
public Player(String name, int age) {
this.name = name;
this.age = age;
}
}
'๊ฐ๋ฐ > Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JAVA 8] ์คํธ๋ฆผ(Stream) (0) | 2022.01.15 |
---|---|
[JAVA 8] ํจ์ํ ์ธํฐํ์ด์ค(Functional Interface) (0) | 2022.01.14 |
[JAVA 8] ๋๋ค ํํ์ (0) | 2022.01.13 |
์๋ฐ Map ์์ฃผ ์ฐ๋ ๋ฉ์๋ ์ ๋ฆฌ (0) | 2021.11.02 |
์๋ฐ ๋ฐฐ์ด โ ๋ฆฌ์คํธ ๋ณํ (Array To List / List To Array) (0) | 2021.11.02 |
๋๊ธ