[Kotlin] ๋ฌธ์ž์—ด ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ๋ฐฐ์น˜ํ•˜๊ธฐ (ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์•Œ๊ณ ๋ฆฌ์ฆ˜) : sortedWith()
๋ฐ˜์‘ํ˜•

 

 

 

 

 

๋ฌธ์ œ

https://school.programmers.co.kr/learn/courses/30/lessons/12917

 

 

 


 

์ ‘๊ทผ

 

  • ๋Œ€๋ฌธ์ž์™€ ์†Œ๋ฌธ์ž๋ฅผ ๋ถ„๋ฆฌ (์•„์Šคํ‚ค ์ฝ”๋“œ ์ด์šฉํ•˜๋ฉด ๋˜์ง€ ์•Š์„๊นŒ ๋Œ€๊ฐ• ์ƒ๊ฐ)
  • ์†Œ๋ฌธ์ž๋ฅผ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ๊ฒƒ๊ณผ, ๋Œ€๋ฌธ์ž๋ฅผ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ๊ฒƒ์„ ํ•ฉ์น˜๊ณ  ๋ฆฌํ„ด
  • ์œ„์™€ ๊ฐ™์ด ์ƒ๊ฐํ–ˆ๋Š”๋ฐ ๋ฌธ์ œ์—์„œ ์›ํ•˜๋Š” ์˜ˆ์ƒ ๋ฆฌํ„ด ๊ฐ’์ด ๋‚ด ์ƒ๊ฐ๊ณผ ์ข€ ๋‹ฌ๋ž๋‹ค.

 

 


 

์„ฑ๊ณต ์ฝ”๋“œ

 

class Solution {
    fun solution(s: String): String 
        = s.toCharArray().sortedDescending().joinToString("")   
}

 


 

์„ค๋ช…

 

  1. ๋Œ€๋ฌธ์ž๋Š” ์›๋ž˜ ์†Œ๋ฌธ์ž๋ณด๋‹ค ๊ฐ’์ด ์ž‘์•„์„œ ์ถ”๊ฐ€ ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š๋‹ค.
  2. ์ฃผ์–ด์ง„ String์„ ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค์–ด ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ๋ฐฐ์—ดํ•˜๊ณ , ๋‹ค์‹œ String์œผ๋กœ ์ด์–ด์ฃผ๋ฉด ๋์ด๋‹ค.

 


 

๋‹ค๋ฅธ ๋ฐฉ๋ฒ•

 

class Solution {
    fun solution(s: String): String = String(s.toCharArray().sortedArrayDescending())
}
  • joinToString("")์„ ์ด์šฉํ•˜์ง€ ์•Š๊ณ , ์• ์ดˆ์— ์ฒ˜์Œ๋ถ€ํ„ฐ String()์„ ์ด์šฉํ•œ ํ˜•์‹์ด๋‹ค.

 

class Solution {
    fun solution(s: String): String {
        return s.toCharArray()
            .sortedWith(Comparator { a, b -> b - a })
            .joinToString("")
    }
}
  • sortedWith()์„ ์ด์šฉํ•œ ํ˜•์‹์ด๋‹ค.
  • sortedWith(Comparator { a, b -> b - a }): ๋ฌธ์ž ๋ฐฐ์—ด์„ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ
    sortedWith() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‚ฌ์šฉ์ž๊ฐ€ ์ œ๊ณตํ•œ ๋น„๊ต์ž(Comparator)์— ๋”ฐ๋ผ ์ •๋ ฌ์„ ์ˆ˜ํ–‰
    b - a๋Š” ๋‘ ์š”์†Œ๋ฅผ ๋น„๊ตํ•˜์—ฌ b๊ฐ€ a๋ณด๋‹ค ํฌ๋ฉด ์–‘์ˆ˜๋ฅผ, ์ž‘์œผ๋ฉด ์Œ์ˆ˜๋ฅผ, ๊ฐ™์œผ๋ฉด 0์„ ๋ฐ˜ํ™˜

 

 

 

 

๐Ÿ’กsortedWith()

fun <T> Iterable<T>.sortedWith(comparator: Comparator<in T>): List<T>
  • ์‚ฌ์šฉ์ž๊ฐ€ ์ œ๊ณตํ•œ ๋น„๊ต์ž(Comparator)์— ๋”ฐ๋ผ ์ปฌ๋ ‰์…˜์„ ์ •๋ ฌ
  • ์ •๋ ฌ๋œ ์ƒˆ๋กœ์šด ์ปฌ๋ ‰์…˜์„ ๋ฐ˜ํ™˜ํ•˜๋ฉฐ, ์›๋ณธ ์ปฌ๋ ‰์…˜์„ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š์Œ
  • comparator: ์ •๋ ฌ์— ์‚ฌ์šฉ๋  ๋น„๊ต์ž
    Comparator ์ธํ„ฐํŽ˜์ด์Šค์˜ compare() ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜์—ฌ ๋‘ ์š”์†Œ๋ฅผ ๋น„๊ต

 

val list = listOf(3, 1, 4, 1, 5, 9)
val sortedList = list.sortedWith(Comparator { a, b -> b - a })
println(sortedList) // ์ถœ๋ ฅ: [9, 5, 4, 3, 1, 1], ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ


val numbers = listOf(5, 3, 8, 2, 7)
val sortedNumbers = numbers.sortedWith(Comparator { a, b -> b.compareTo(a) })
println(sortedNumbers) // ์ถœ๋ ฅ: [8, 7, 5, 3, 2]


val words = listOf("apple", "banana", "orange", "kiwi")
val sortedWords = words.sortedWith(compareBy { it.length })
println(sortedWords) // ์ถœ๋ ฅ: [kiwi, apple, banana, orange], ๋ฌธ์ž ๊ธธ์ด์— ๋”ฐ๋ฅธ ์ •๋ ฌ


data class Person(val name: String, val age: Int)
val people = listOf(Person("Alice", 30), Person("Bob", 25), Person("Charlie", 35))
val sortedPeople = people.sortedWith(compareBy<Person> { it.age }.thenBy { it.name })
println(sortedPeople) 
// ์ถœ๋ ฅ: [Person(name=Bob, age=25), Person(name=Alice, age=30), Person(name=Charlie, age=35)]

 

 

 

 

 

 

 

 

 

๋ฐ˜์‘ํ˜•
 ๐Ÿ’ฌ C O M M E N T