JiYoung Dev 🖥

[JAVA] List 정렬 - sort() 본문

Question

[JAVA] List 정렬 - sort()

Shinjio 2023. 8. 24. 09:09

.sort()

- 오름차순 정렬 : Comparator.naturalOrder()

- 내림차순 정렬 : Comparator.reverseOrder()

 

 

    public Integer[] solution(int[] numbers) {
        
        ArrayList<Integer> arr = new ArrayList<Integer>();
        
        int idx = 0;
        for(int i = 0 ; i < numbers.length ; i++){
            for(int j = i + 1 ; j < numbers.length ; j++){
                int sum = numbers[i] + numbers[j];
            
                if(!arr.contains(sum)) {
                	arr.add(sum);
                };
            }
        }
        arr.sort(Comparator.naturalOrder());
        
        Integer[] answer = arr.toArray(new Integer[arr.size()]);
        
       
        return answer;
    }