[Java] List형 쉽게 정렬하는 방법

    간혹 프로젝트를 하다보면, 정렬을 어렵게 수행하는 사람들을 보는 경우가 있다. 예를 들어 sort algorithm을 별도로 만들어서 리스트 데이터를 정렬하는 경우를 본적이 있는데, 복잡한 구조라면 필요한 경우도 있겠지만 간단한 데이터 타입의 리스트 구조마저 자신들이 만들어 놓은 알고리즘으로 정렬을 하는 말그대로 삽X을 하는 경우를 종종 본적이 있다.


    JAVA에서는 매우 간단하게 정렬을 할 수 있는 방법을 제공하니, 본인의 알고리즘을 믿지 말고 제공해주는 라이브러리를 활용하는 것이 현명하겠다.




    List 정렬하기


    java.util 라이브러리 안에는 Collections라는 하위 라이브러리가 존재한다. 이놈은 여러가지 다양한 기능을 제공하는데(정렬, 셔플 등등) 정렬에 대해서만 정리해보도록 하겠다.


    오름차순 (Ascending)

    Collections.sort(list데이터);


    내림차순 (Descending)

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



    문자형 예제

    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
    27
    28
    29
    30
    31
    32
    33
    34
    public static void main(String[] args) throws InterruptedException {
            List<String> temp = new ArrayList<String> ();
            temp.add("가나다라");
            temp.add("동해물과");
            temp.add("나랏말싸미");
            temp.add("abc초콜렛");
            
            sort(temp);
        }    
        
        
    public static void sort(List<String> temp) {
            System.out.println("=======================================");
            for(String str : temp) {
                System.out.println(str);
            }
            
            // sort 로직
            Collections.sort(temp);
            
            System.out.println("=======================================");
            for(String str : temp) {
                System.out.println(str);
            }
            
            // sort 역순
            Collections.sort(temp, Collections.reverseOrder());
            
            System.out.println("=======================================");
            for(String str : temp) {
                System.out.println(str);
            }
    }
        
    cs

    =======================================

    가나다라

    동해물과

    나랏말싸미

    abc초콜렛

    =======================================

    abc초콜렛

    가나다라

    나랏말싸미

    동해물과

    =======================================

    동해물과

    나랏말싸미

    가나다라

    abc초콜렛



    정수형 예제

    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
    27
    28
    29
    30
    31
    32
    33
    public static void main(String[] args) throws InterruptedException {
            List<Integer> intTemp = new ArrayList<Integer> ();
            intTemp.add(27237237);
            intTemp.add(12762312);
            intTemp.add(23610212);
            intTemp.add(12863523);
            
            sortInt(intTemp);
        }    
        
        
    public static void sortInt(List<Integer> temp) {
            System.out.println("=======================================");
            for(Integer value : temp) {
                System.out.println(value);
            }
            
            // sort 로직
            Collections.sort(temp);
            
            System.out.println("=======================================");
            for(Integer value : temp) {
                System.out.println(value);
            }
            
            // sort 역순
            Collections.sort(temp, Collections.reverseOrder());
            
            System.out.println("=======================================");
            for(Integer value : temp) {
                System.out.println(value);
            }
    }
    cs


    =======================================

    27237237

    12762312

    23610212

    12863523

    =======================================

    12762312

    12863523

    23610212

    27237237

    =======================================

    27237237

    23610212

    12863523

    12762312


    이와같이 매우 쉬운 방법으로 정렬을 처리할 수 있으니, 왠만해서는 라이브러리를 사용하는 것이 좋겠다.

    댓글

    Designed by JB FACTORY