ArrayList / 배열 정렬

알파·2022년 10월 14일
0
post-custom-banner

import java.util.*;

ArrayList 정렬

1. Collections.sort()

Collections.sort(list);

Collections.sort(list, Collections.reverseOrder());

2. List.sort()

list.sort(Comparator.naturalOrder());

list.sort(Comparator.reverseOrder());

배열 정렬

1. Arrays.sort(arr);

Arrays.sort(arr);

Arrays.sort(arr, Collections.reverseOrder());

Map 정렬

    List<String> keys = new ArrayList<>(map.keySet());
    Collections.sort(keys);
    for (String key : keys) {
        System.out.println(String.format("Key : %s, Value : %s", key, map.get(key)));
    }

    keys.sort(Collections.reverseOrder());
    for (String key : keys) {
        System.out.println(String.format("Key : %s, Value : %s", key, map.get(key)));
    }
profile
I am what I repeatedly do
post-custom-banner

0개의 댓글