๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Android (Kotlin)/kotlin ๋ฌธ๋ฒ•

[Collection Sort - kotlin] sort(), sortBy(), sortWith()

by ํ‚ค์œค 2023. 11. 30.

๐Ÿ’ก๊ธฐ๋ณธ ๊ทœ์น™

sort(), sortBy(), sortWith() ๋Š” ๊ธฐ์กด collection์„ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”์†Œ๋“œ์ด๋‹ค.

sorted(), sortedBy(), sortedWith() ๋Š” ๊ธฐ์กด collection์ด ๋ณ€ํ•˜์ง€ ์•Š๋Š”๋‹ค.
์•ž์— ๋‚˜์˜จ ๋™์‚ฌ๊ฐ€ ์ˆ˜๋™ํƒœ๊ฐ€ ๋˜๋ฉด ๊ธฐ์กด collection์ด ๋ณ€ํ•˜์ง€ ์•Š๋Š”๋‹ค๊ณ  ๋ณด๋ฉด ๋ ๋“ฏ! ๋’ค์— reverse(), reversed(), sortByDescending(), sortedByDescending() ๋„ ๋ชจ๋‘ ๊ฐ™์€ ๊ทœ์น™ ์ ์šฉ!

๐Ÿ“† ๋ฉ”์†Œ๋“œ ์ •์˜

sort() - ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
sortByDescending() - ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ
reverse() - ์•ž๋’ค ๋ฐฐ์—ด ์ˆœ์„œ ๋’ค์ง‘๊ธฐ
shuffled() - ๋žœ๋ค ์ •๋ ฌ

Kotlin sort

1. ์˜ค๋ฆ„์ฐจ์ˆœ

val nums = mutableListOf(3, 1, 7, 2, 8, 6)
	
nums.sort()
// nums: [1, 2, 3, 6, 7, 8]

2. ๋‚ด์ž„์ฐจ์ˆœ

nums.sortDescending();
// nums: [8, 7, 6, 3, 2, 1]

Kotlin sorted

1. ์˜ค๋ฆ„์ฐจ์ˆœ

val nums = mutableListOf(3, 1, 7, 2, 8, 6)
val sortedNums = nums.sorted()

// nums: [3, 1, 7, 2, 8, 6]
// sortedNums: [1, 2, 3, 6, 7, 8]

2. ๋‚ด์ž„์ฐจ์ˆœ

val sortedNumsDescending = nums.sortedDescending()

// nums: [3, 1, 7, 2, 8, 6]
// sortedNumsDescending: [8, 7, 6, 3, 2, 1]

Kotlin sortBy

1. ์˜ค๋ฆ„์ฐจ์ˆœ

val myDates = mutableListOf(
	MyDate(4, 3),
	MyDate(5, 16),
	MyDate(1, 29)
)

myDates.sortBy { it.month }
myDates.forEach { println(it) }
/*
MyDate(month=1, day=29)
MyDate(month=4, day=3)
MyDate(month=5, day=16)
*/

2. ๋‚ด์ž„์ฐจ์ˆœ

myDates.sortByDescending { it.month }
myDates.forEach { println(it) }
/*
MyDate(month=5, day=16)
MyDate(month=4, day=3)
MyDate(month=1, day=29)
*/

Kotlin sortedBy

1. ์˜ค๋ฆ„์ฐจ์ˆœ

val myDates = mutableListOf(
	MyDate(4, 3),
	MyDate(5, 16),
	MyDate(1, 29)
)

val sortedDates = myDates.sortedBy { it.month }
myDates.forEach { println(it) }
/*
MyDate(month=4, day=3)
MyDate(month=5, day=16)
MyDate(month=1, day=29)
*/
sortedDates.forEach { println(it) }
/*
MyDate(month=1, day=29)
MyDate(month=4, day=3)
MyDate(month=5, day=16)
*/

2. ๋‚ด์ž„์ฐจ์ˆœ

val sortedDatesDescending = myDates.sortedByDescending { it.month }
myDates.forEach { println(it) }
/*
MyDate(month=4, day=3)
MyDate(month=5, day=16)
MyDate(month=1, day=29)
*/
sortedDatesDescending.forEach { println(it) }
/*
MyDate(month=5, day=16)
MyDate(month=4, day=3)
MyDate(month=1, day=29)
*/

Kotlin sortWith

1. ์˜ค๋ฆ„์ฐจ์ˆœ

val myDates = mutableListOf(
	MyDate(8, 19),
	MyDate(5, 16),
	MyDate(1, 29),
	MyDate(5, 10),
	MyDate(8, 3)
)

myDates.sortWith(compareBy { it.month }.thenBy { it.day })
myDates.forEach { println(it) }
/*
MyDate(month=1, day=29)
MyDate(month=5, day=10)
MyDate(month=5, day=16)
MyDate(month=8, day=3)
MyDate(month=8, day=19)
*/

2. ๋‚ด๋ฆผ์ฐจ์ˆœ

myDates.reverse()
sortedDatesDescending.forEach { println(it) }
/*
MyDate(month=8, day=19)
MyDate(month=8, day=3)
MyDate(month=5, day=16)
MyDate(month=5, day=10)
MyDate(month=1, day=29)
*/

Kotlin sortedWith

1. ์˜ค๋ฆ„์ฐจ์ˆœ

val sortedDates = myDates.sortedWith(compareBy { it.month }.thenBy { it.day })
myDates.forEach { println(it) }
/*
MyDate(month=8, day=19)
MyDate(month=5, day=16)
MyDate(month=1, day=29)
MyDate(month=5, day=10)
MyDate(month=8, day=3)
*/
sortedDates.forEach { println(it) }
/*
MyDate(month=1, day=29)
MyDate(month=5, day=10)
MyDate(month=5, day=16)
MyDate(month=8, day=3)
MyDate(month=8, day=19)
*/

2. ๋‚ด๋ฆผ์ฐจ์ˆœ

val sortedDatesDescending = myDates.sortedWith(compareBy { it.month }.thenBy { it.day }).reversed()
myDates.forEach { println(it) }
/*
MyDate(month=8, day=19)
MyDate(month=5, day=16)
MyDate(month=1, day=29)
MyDate(month=5, day=10)
MyDate(month=8, day=3)
*/
sortedDatesDescending.forEach { println(it) }
/*
MyDate(month=8, day=19)
MyDate(month=8, day=3)
MyDate(month=5, day=16)
MyDate(month=5, day=10)
MyDate(month=1, day=29)
*/

 

โฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ’€์ด

https://occhiolism.tistory.com/31

 

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค • ์ฝ”ํ‹€๋ฆฐ] ๋ฌธ์ž์—ด ๋‚ด ๋งˆ์Œ๋Œ€๋กœ ์ •๋ ฌํ•˜๊ธฐ #12915

#12915 ๐ŸŽ„ Question ? https://school.programmers.co.kr/learn/courses/30/lessons/12915 class Solution { fun solution(strings: Array, n: Int): Array { var answer = arrayOf() return answer } } ๐Ÿงฉ Thought Process for๋ฌธ ๋Œ๋ ค์„œ n๋ฒˆ์งธ ์ธ๋ฑ์Šค์˜ ์•ŒํŒŒ

occhiolism.tistory.com

์ถœ์ฒ˜: https://www.bezkoder.com/kotlin-sort-list/#Kotlin_sorted

bezcode ์›น์‚ฌ์ดํŠธ๊ฐ€ ์„ค๋ช…์ด ์ •๋ง ์ž˜ ๋‚˜์™€์žˆ๋‹ค :))