알고리즘/Programmers

[Swift_Programmes] 정수 내림차순으로 배치하기

YEN_ 2023. 11. 21. 09:04

 

 

프로그래머스 문제 링크 :https://school.programmers.co.kr/learn/courses/30/lessons/12933


풀이    

func solution(_ n:Int64) -> Int64 {
    return Int64(String(String(n).sorted(by: >)))!
}

 

  • String(n).sorted(by: >)  -> 배열
  • String(String(n).sorted(by: >))   -> 문자열로 바꿔주기
  • Int64(String(String(n).sorted(by: >)))   ->  반환형 맞춰주기 / 옵셔널로 리턴됨
    • 강체추출 ! 를 해준다

중요 개념    

  • 내림차순, 오름차순으로 정렬할 수 있는 메소드 sorted(by:) 를 이용한다
  • sorted(by: >) -> 내림차순으로 정렬

https://developer.apple.com/documentation/swift/array/sorted()

 

sorted() | Apple Developer Documentation

Returns the elements of the sequence, sorted.

developer.apple.com