ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•˜๋‹ค๋ณด๋ฉด DB ์กฐํšŒ ์‹œ์— ์ •๋ ฌํ•ด์„œ ์กฐํšŒํ•ด์˜ค๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๊ณ , ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ ˆ๋ฒจ์—์„œ ์ •๋ ฌํ•ด์•ผ ํ•  ๋•Œ๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ค๋Š˜์€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ๋ฐฐ์—ด, ๋ฆฌ์ŠคํŠธ, ๋งต, ์…‹์— ๋Œ€ํ•˜์—ฌ ๊ฐ๊ฐ ์˜ค๋ฆ„์ฐจ์ˆœ, ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

1. ๋ฐฐ์—ด ์ •๋ ฌํ•˜๊ธฐ

์šฐ์„  ์•„๋ž˜์™€ ๊ฐ™์ด int ๋ฐฐ์—ด์ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

int[] arr = new int[] { 5, 1, 89, 255, 7, 88, 200, 123, 66 };

๋ฐฐ์—ด์˜ ์ •๋ ฌ์€ java.util.Arrays ํด๋ž˜์Šค์— ์žˆ๋Š” sort() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜๋ฉด ๋ชจ๋“  primitive ํƒ€์ž…์— ๋Œ€ํ•œ ์ •๋ ฌ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋ฌผ๋ก  ์›์‹œํƒ€์ž…์ด ์•„๋‹Œ Object[] ๋„ ์ •๋ ฌ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

int[] arr = new int[] { 5, 1, 89, 255, 7, 88, 200, 123, 66 };
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));

Arrays.sort(๋ฐฐ์—ด) ํ˜ธ์ถœ์„ ํ•˜๋ฉด ์ธ์ž๋กœ ๋„˜๊ฒจ์ค€ ๋ฐฐ์—ด์„ ์ •๋ ฌํ•ด์ค๋‹ˆ๋‹ค. 

๊ทธ๋ฆฌ๊ณ  ์ถœ๋ ฅ์„ ํ•ด๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ž˜ ์ •๋ ฌ์ด ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

[1, 5, 7, 66, 88, 89, 123, 200, 255]

๊ทธ๋Ÿผ ์ด ๋ฉ”์„œ๋“œ๊ฐ€ ๋‚ด๋ถ€์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์–ด๋–ค ๊ฒƒ์ผ๊นŒ์š”?

Java8 ๋ฌธ์„œ์—๋Š” ๋“€์–ผ ํ”ผ๋ด‡ ํ€ต์†ŒํŠธ(Dual-Pivot Quick Sort) ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ๋‚˜์™€์žˆ์Šต๋‹ˆ๋‹ค.

๋“€์–ผ ํ”ผ๋ด‡ ํ€ต์†ŒํŠธ๋Š” ๋ชจ๋“  ๋ฐ์ดํ„ฐ ์…‹์— ๋Œ€ํ•ด์„œ O(n log(n)) ์˜ ํผํฌ๋จผ์Šค๋ฅผ ๋ณด์ด๊ณ  ์ผ๋ฐ˜์ ์œผ๋กœ ๊ธฐ์กด์˜ One-Pivot ํ€ต์†ŒํŠธ ์ •๋ ฌ๋ณด๋‹ค ๋น ๋ฅด๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

Java7 ๋ฌธ์„œ, Java14 ๋ฌธ์„œ์—๋„ ๋™์ผํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๋‚˜์™€์žˆ์Šต๋‹ˆ๋‹ค.

1.1 ๋ถ€๋ถ„์ •๋ ฌ

์œ„ ์˜ˆ์ œ๋Š” ๋ฐฐ์—ด์˜ ์ „์ฒด ์š”์†Œ๋ฅผ ์ •๋ ฌํ•˜๋Š” ๋ฐ˜๋ฉด ๋ฐฐ์—ด์˜ ์ผ๋ถ€๋ถ„๋งŒ ์ •๋ ฌ์„ ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

์ผ๋ถ€๋ถ„๋งŒ ์ •๋ ฌํ•  ๋•Œ๋Š” Arrays.sort(int[] a, int fromIndex, int toIndex) ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

int[] arr = new int[] { 5, 1, 89, 255, 7, 88, 200, 123, 66 };
Arrays.sort(arr, 0, 4);
System.out.println(Arrays.toString(arr));

์ถœ๋ ฅ์„ ํ•ด๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด 0๋ฒˆ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ 3๋ฒˆ ์ธ๋ฑ์Šค๊นŒ์ง€(4๋Š” exclude์ž…๋‹ˆ๋‹ค) ์ •๋ ฌ์ด ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

[1, 5, 89, 255, 7, 88, 200, 123, 66]

 

1.2 ๋ณ‘๋ ฌ ์ •๋ ฌ (ParallelSort)

Arrays.sort()๊ฐ€ ์‹ฑ๊ธ€์“ฐ๋ ˆ๋“œ๋กœ ๋Œ์•„๊ฐ„๋‹ค๋ฉด Arrays.parallelSort()๋Š” ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ๋กœ ๋™์ž‘์„ ํ•ฉ๋‹ˆ๋‹ค. parallelSort๋Š” ๋ฐฐ์—ด์„ sub-array๋กœ ๋‚˜๋ˆˆ ๋’ค ๊ฐ๊ฐ์„ ๋ณ„๋„์˜ ์“ฐ๋ ˆ๋“œ์—์„œ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๊ณ  ๊ฐ ์“ฐ๋ ˆ๋“œ์—์„œ๋Š” Arrays.sort()๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ์ •๋ ฌ์„ ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

Arrays.parallelSort ์˜ ๊ฒฐ๊ณผ๋Š” ๋‹น์—ฐํžˆ Arrays.sort์˜ ๊ฒฐ๊ณผ์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋”ฉ์„ ์ด์šฉํ–ˆ๋Š๋ƒ์˜ ์ฐจ์ด๋งŒ ์กด์žฌํ•  ๋ฟ์ž…๋‹ˆ๋‹ค.

์ฐธ๊ณ ๋กœ Arrays.parallelSort ๋„ ๋ถ€๋ถ„์ •๋ ฌ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. Arrays.parallelSort (int [] a, int fromIndex, int toIndex);

2. List ์ •๋ ฌํ•˜๊ธฐ

๋ฆฌ์ŠคํŠธ๋ฅผ ์ •๋ ฌํ•  ๋•Œ๋Š” java.util.Collections.sort(List<T> list) ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค.

int[] arr = new int[] { 5, 1, 89, 255, 7, 88, 200, 123, 66 };
List<Integer> toSortList = Ints.asList(arr);    // int[]์„ Integer[]๋กœ ๋ณ€ํ™˜ (๊ตฌ๊ธ€ guava ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ด์šฉ) 
Collections.sort(toSortList);

์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” Arrays.sort()๋ฅผ ์‚ฌ์šฉํ•œ ๊ฒƒ๊ณผ ๋™์ผํ•ฉ๋‹ˆ๋‹ค.

๊ธฐ๋ณธ์ ์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ์„ ํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด comparator๋ฅผ ์ธ์ž๋กœ ์ „๋‹ฌ๋ฐ›๋Š” sortโ€‹(List list, Comparator c) ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋„ˆ๋ฌด๋‚˜๋„ ๊ธฐ๋ณธ์ ์ธ ์–˜๊ธฐ์ง€๋งŒ ์ •๋ ฌ์„ ํ•˜๋ ค๋ฉด ๋‘ ์š”์†Œ(element)๋ฅผ ๋น„๊ตํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฆฌ์ŠคํŠธ ๋‚ด์˜ ๋ชจ๋“  ์š”์†Œ๋“ค์ด Comparable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

Collections.sort(List<T> list) ๋Š” merge sort ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด์šฉํ•˜์—ฌ ์ •๋ ฌ์„ ํ•ฉ๋‹ˆ๋‹ค. List.sort(Comparator c)์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋ณธ ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ๋Š” ์กฐ๊ธˆ ๋‹ค๋ฅด๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค๋งŒ, ๋” ์•ˆ์ข‹๊ฒŒ ๋ณ€ํ˜•๋œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฑด ์•„๋‹ˆ๊ฒ ์ฃ . ๊ทธ๋ฆฌ๊ณ  ๊ธฐ๋ณธ์ ์œผ๋กœ List.sort()์—์„œ ์‚ฌ์šฉํ•˜๋Š” merge sort๋Š” ์–ด๋Š์ •๋„ ๋ถ€๋ถ„ ์ •๋ ฌ์ด ๋˜์–ด์žˆ๋Š” ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ๋Š” n log(n) ๋ณด๋‹ค ํ›จ์”ฌ ์ ์€ ํšŸ์ˆ˜๋งŒ ๋น„๊ตํ•˜๋ฉฐ(๊ฑฐ์˜ ์ •๋ ฌ์ด ๋˜์–ด์žˆ๋Š” ์ƒํƒœ๋ผ๋ฉด ๊ฑฐ์˜ n๋ฒˆ์˜ ๋น„๊ต๋งŒ์œผ๋กœ๋„ ์ •๋ ฌ ๊ฐ€๋Šฅ), ๋žœ๋คํ•˜๊ฒŒ ์„ž์—ฌ์žˆ๋Š” ๋ฐ์ดํ„ฐ ์…‹์— ๋Œ€ํ•ด์„œ๋Š” ์ „ํ†ต์ ์ธ merge sort์˜ ํผํฌ๋จผ์Šค๋ฅผ ๋‚ธ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. Java14 Collections.sort() ์ฐธ๊ณ ํ•˜์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

๋งŒ์•ฝ ์—ญ์ˆœ(๋‚ด๋ฆผ์ฐจ์ˆœ)์œผ๋กœ ์ •๋ ฌ์„ ํ•˜๊ณ  ์‹ถ์œผ๋ฉด Collections.sort(List list, Comparator c) ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

// ๋‚ด๋ฆผ ์ฐจ์ˆœ ์ •๋ ฌ
Collections.sort(list, Comparator.reverseOrder());

์ž๋ฐ”์—์„œ๋Š” ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ์„ ์œ„ํ•ด Comparator.reverseOrder() ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ๊ธฐ๋ณธ comparator๋ฅผ ์ œ๊ณตํ•ด์ฃผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. 

3. Set ์ •๋ ฌํ•˜๊ธฐ

Set์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ˆœ์„œ๋ฅผ ๋ณด์žฅํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ LinkedHashSet์€ ์ˆœ์„œ๋ฅผ ๋ณด์žฅํ•˜์ฃ . ๋”ฐ๋ผ์„œ Set์„ ์ •๋ ฌํ•œ๋‹ค๋Š” ๊ฒƒ์€ ์ด LinkedHashSet ์ฒ˜๋Ÿผ ์ˆœ์„œ๋ฅผ ๋ณด์žฅํ•˜๋Š” set ๊ตฌํ˜„์ฒด์— ๋Œ€ํ•œ ์–˜๊ธฐ์ž…๋‹ˆ๋‹ค.

Set์„ ์ •๋ ฌํ•  ๋•Œ๋„ Collections.sort()๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ํ•ด๋‹น ๋ฉ”์„œ๋“œ๋Š” List๋งŒ์„ ์ธ์ž๋กœ ์ „๋‹ฌ๋ฐ›๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— Set์„ ์ •๋ ฌํ•˜๋ ค๋ฉด List๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ์ž‘์—…์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

Set<Integer> set = new LinkedHashSet<>(Ints.asList(arr));
System.out.println(set);    // ์ •๋ ฌํ•˜๊ธฐ ์ „ ์ถœ๋ ฅ

List<Integer> list = new ArrayList<>(set);
Collections.sort(list);
set = new LinkedHashSet<>(list);

System.out.println(set);    // ์ •๋ ฌ ํ›„ ์ถœ๋ ฅ

์œ„์—์„œ ๋ณด๋“ฏ์ด list๋กœ ๋ณ€ํ™˜ ํ›„ ๋‹ค์‹œ set์œผ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ์ •๋ ฌ์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

4. Map ์ •๋ ฌํ•˜๊ธฐ

Map์€ ์•„์‹œ๋‹ค์‹œํ”ผ key์™€ value ์Œ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ์ž๋ฃŒ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ Map์„ ์ •๋ ฌํ•  ๋•Œ๋Š” key๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ value๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜๋Š” ๋ฐฉ๋ฒ•, ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ, Map๋„ Set๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ HashMap ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” ์ˆœ์„œ์™€๋Š” ๋ฌด๊ด€ํ•œ ์ž๋ฃŒ๊ตฌ์กฐ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ •๋ ฌ์„ ์œ„ํ•ด์„œ๋Š” LinkedHashMap์„ ์ด์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

์šฐ์„  key๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. Map์„ ์ •๋ ฌํ•  ๋•Œ๋„ List, Set๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ Collections.sort(List list) ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ list๋กœ ๋ณ€ํ™˜์„ ํ•ด์ฃผ์–ด์•ผ ํ•˜์ฃ . ๊ทธ๋ฆฌ๊ณ  ์ •๋ ฌ ํ›„์—๋Š” ์ˆœ์„œ์˜ ๋ณด์žฅ์„ ์œ„ํ•ด list๋ฅผ LinkedHashMap์œผ๋กœ ๋ณ€ํ™˜ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ List.sort(Comparator c)๋ฅผ ์ด์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ์ œ์—์„œ ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ๋ชจ๋‘ ํ™•์ธํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

// ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ์…‹
HashMap<Integer, String> map = new HashMap<>();
map.put(5, "Orange");
map.put(8, "Apple");
map.put(2, "WaterMelon");
map.put(13, "Pear");
map.put(9, "Grape");
map.put(4, "Banana");


// key, value ๋ฅผ ๋ชจ๋‘ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•ด์„œ entrySet()์„ ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜
List<Map.Entry<Integer, String>> entries = new ArrayList<>(map.entrySet());

// 1. List.sort ๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•
entries.sort(Comparator.comparing(Map.Entry::getKey));

// 2. Collections.sort ๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•
Collections.sort(entries, Comparator.comparing(Map.Entry::getKey));

// ์ •๋ ฌ๋œ ๋ฐ์ดํ„ฐ๋ฅผ LinkedHashMap์— ์ €์žฅ
Map<Integer, String> sortedByKey = new LinkedHashMap<>();
for (Map.Entry<Integer, String> entry : entries) {
	sortedByKey.put(entry.getKey(), entry.getValue());
}

// ์ •๋ ฌ๋œ ๋‚ด์šฉ ์ถœ๋ ฅ
System.out.println(sortedByKey);

์ •๋ ฌํ•  ๋•Œ List.sort(Comparator c) ์™€ Collections.sort(List list)๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ชจ๋‘ ์†Œ๊ฐœํ•ด๋“œ๋ ธ์Šต๋‹ˆ๋‹ค.

์—ฌ๊ธฐ์„œ ์‚ฌ์šฉํ•œ Comparator.comparing() ๋ฉ”์„œ๋“œ๋Š” comparator๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. Comparator.comparing(Map.Entry::getKey) ๋Š” Map์˜ key ๊ฐ’์„ ๋น„๊ตํ•˜๋Š” comparator๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์ฃ .

์œ„ ์ฝ”๋“œ์—์„œ ์ถœ๋ ฅ๋˜๋Š” ๋‚ด์šฉ์€ ์•„๋ž˜์™ ๊ฐ™์Šต๋‹ˆ๋‹ค.

{2=WaterMelon, 4=Banana, 5=Orange, 8=Apple, 9=Grape, 13=Pear}

key ๊ฐ’ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌ์ด ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๊ทธ๋Ÿผ ์ด๋ฒˆ์—๋Š” ์œ„ ์ฝ”๋“œ๋ฅผ ์กฐ๊ธˆ ์ˆ˜์ •ํ•˜์—ฌ value๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ์„ ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

// 1. List.sort ๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•
entries.sort(Comparator.comparing(Map.Entry::getValue));

// 2. Collections.sort ๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•
Collections.sort(entries, Comparator.comparing(Map.Entry::getValue));

Comparator.comparing() ๋ฉ”์„œ๋“œ์˜ ์ธ์ž๋กœ key๊ฐ€ ์•„๋‹Œ value ๊ฐ’์„ ๋„˜๊ฒจ์ฃผ๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์ˆ˜์ •๋œ ์ฝ”๋“œ ์‹คํ–‰ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

{8=Apple, 4=Banana, 9=Grape, 5=Orange, 13=Pear, 2=WaterMelon}

value๊ฐ’์œผ๋กœ ๋„ฃ์–ด์ฃผ์—ˆ๋˜ ๊ณผ์ผ๋ช… ์ˆœ์œผ๋กœ ์ •๋ ฌ์ด ๋˜์–ด ์ถœ๋ ฅ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

5. ์ปค์Šคํ…€ ๊ฐ์ฒด ์ •๋ ฌํ•˜๊ธฐ

์ด๋ฒˆ์—๋Š” ์ปค์Šคํ…€ ๊ฐ์ฒด๋ฅผ ์ •๋ ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์‚ฌ์‹ค ์ปค์Šคํ…€ ๊ฐ์ฒด๋ฅผ ์ •๋ ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋ผ๊ณ  ๋”ฐ๋กœ ์žˆ๋Š”๊ฑด ์•„๋‹™๋‹ˆ๋‹ค. ์ด๋ฏธ ์œ„์—์„œ ์„ค๋ช…๋“œ๋ฆฐ ๋‚ด์šฉ์ค‘์— comparator๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ด์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ผ๋‹จ ์‡ผํ•‘๋ชฐ์— ํŒ๋งค์ค‘์ธ ์ƒํ’ˆ์— ๋Œ€ํ•œ ๊ฐ์ฒด ์ •์˜๋ฅผ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

@Getter
@Setter
@AllArgsConstructor
class Product {
    String name;        // ์ƒํ’ˆ ๋ช…
    int price;          // ์ƒํ’ˆ ๊ฐ€๊ฒฉ
    float sellerRating; // ํŒ๋งค์ž ํ‰์ 
    
    @Override
    public String toString() {
    	return "{" + this.name + ", " + price + ", " + sellerRating + "}";
    }
}

์ด์ œ ์ด ๊ฐ์ฒด๋ฅผ ๋ฆฌ์ŠคํŠธ๋กœ ๋งŒ๋“ค์–ด ๊ฐ€๊ฒฉ, ํŒ๋งค์ž ํ‰์ ์„ ๊ธฐ์ค€์œผ๋กœ ๊ฐ๊ฐ ์˜ค๋ฆ„์ฐจ์ˆœ, ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ์„ ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

// ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ์…‹
List<Product> data = new ArrayList<>(Arrays.asList(
    new Product("Apple", 100, 4.3f),
    new Product("Apple", 200, 3.3f),
    new Product("Apple", 150, 4.8f)));

// 1. List.sort ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ€๊ฒฉ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
data.sort(Comparator.comparing(Product::getPrice));
System.out.println("\n1. List.sort ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ€๊ฒฉ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ");
System.out.println(data);

// 2. List.sort ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ€๊ฒฉ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ
data.sort(Comparator.comparing(Product::getPrice, Comparator.reverseOrder()));
System.out.println("\n2. List.sort ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ€๊ฒฉ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ");
System.out.println(data);

// 3. Collections.sort ๋ฅผ ์ด์šฉํ•˜์—ฌ ํŒ๋งค์ž ํŽธ์  ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
Collections.sort(data, Comparator.comparing(Product::getSellerRating));
System.out.println("\n3. Collections.sort ๋ฅผ ์ด์šฉํ•˜์—ฌ ํŒ๋งค์ž ํŽธ์  ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ");
System.out.println(data);

// 4. Collections.sort ๋ฅผ ์ด์šฉํ•˜์—ฌ ํŒ๋งค์ž ํŽธ์  ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ
Collections.sort(data, Comparator.comparing(Product::getSellerRating, Comparator.reverseOrder()));
System.out.println("\n4. Collections.sort ๋ฅผ ์ด์šฉํ•˜์—ฌ ํŒ๋งค์ž ํŽธ์  ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ");
System.out.println(data);

Product ๋ฆฌ์ŠคํŠธ๋ฅผ ํŠน์ • ํ•„๋“œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๋Š” ์ฝ”๋“œ๋Š” List.sort๋ฅผ ์‚ฌ์šฉํ•ด๋„, Collections.sort๋ฅผ ์‚ฌ์šฉํ•ด๋„ ํ•œ ์ค„์ด๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋งค์šฐ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ์„ ํ•˜๊ณ  ์žˆ์„ ๋•Œ๋Š” Comparator.comparing() ๋ฉ”์„œ๋“œ์— Comparator.reverseOrder() ์ธ์ž๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, Comparator.comparing(์ •๋ ฌ๊ธฐ์ค€, ์ •๋ ฌ์ˆœ์„œ) ์ด๋ ‡๊ฒŒ ์‚ฌ์šฉํ•˜์‹œ๋ฉด ๋˜๋Š”๊ฑฐ์ฃ .

 

์ž, ์ด์ œ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

1. List.sort ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ€๊ฒฉ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
[{Apple, 100, 4.3}, {Apple, 150, 4.8}, {Apple, 200, 3.3}]

2. List.sort ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ€๊ฒฉ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ
[{Apple, 200, 3.3}, {Apple, 150, 4.8}, {Apple, 100, 4.3}]

3. Collections.sort ๋ฅผ ์ด์šฉํ•˜์—ฌ ํŒ๋งค์ž ํŽธ์  ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
[{Apple, 200, 3.3}, {Apple, 100, 4.3}, {Apple, 150, 4.8}]

4. Collections.sort ๋ฅผ ์ด์šฉํ•˜์—ฌ ํŒ๋งค์ž ํŽธ์  ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ
[{Apple, 150, 4.8}, {Apple, 100, 4.3}, {Apple, 200, 3.3}]

Summary

์—ฌ๊ธฐ๊นŒ์ง€ ์ž๋ฐ”์—์„œ ์ •๋ ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค.

๊ฐ„๋žตํžˆ ์š”์•ฝ์„ ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

๋ฐฐ์—ด์˜ ์ •๋ ฌ -> Arrays.sort() ์ด์šฉ

List ์ •๋ ฌ -> Collections.sort(), List.sort() ์ด์šฉ

Set, Map ์ •๋ ฌ -> List๋กœ ๋ณ€ํ™˜ ํ›„ Collections.sort(), List.sort() ์ด์šฉ

๊ธฐ๋ณธ์ ์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ

๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ์€ Comparator.reverseOrder() ์ด์šฉ