[Java] 숫자형 배열 오름차순 내림차순 정렬하기

샘플코드

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
35
public static void main(String[] args) { 
	int[] A = new int[]{3, 2, -2, 5, -3};
	
	// 양수
	// positivies : [5, 3, 2]
	int[] positivies = IntStream.of(A).boxed().sorted(Comparator.reverseOrder())
	    .filter(o -> o >= 0)
	    .mapToInt(i -> i)
	    .toArray();
	
	// 음수
	// negatives : [-2, -3]
	int[] negatives = IntStream.of(A).boxed().sorted(Comparator.reverseOrder())
	    .filter(o -> o < 0)
	    .mapToInt(i -> i)
	    .toArray();
	
	// 내림차순
	// descOrder : [5, 3, 2, -2, -3]
	int[] descOrder = IntStream.of(A).boxed().sorted(Comparator.reverseOrder())
		    .mapToInt(i -> i)
		    .toArray();
	
	// 오름차순
	// ascOrder : [-3, -2, 2, 3, 5]
	int[] ascOrder = IntStream.of(A).boxed().sorted(Comparator.naturalOrder())
		    .mapToInt(i -> i)
		    .toArray();
	    
	for(int value : positivies) {
		if(IntStream.of(negatives).anyMatch(o -> o == -value)) {
			System.out.println("value : "+value);
		}
	}
} 


Tags:

Updated:

Leave a comment