dev._.note

[Swift] sort와 sorted의 차이점 본문

Dev/SWIFT

[Swift] sort와 sorted의 차이점

Laena 2023. 11. 21. 19:10

sort는 단어의 뜻대로 정렬해 주는 메서드이다.

 


 

sort의 종류

sort()
sort(by: )
sorted()
sorted(by: )

 

 


sort()

공식문서 : https://developer.apple.com/documentation/swift/array/sort()

 

sort() | Apple Developer Documentation

Sorts the collection in place.

developer.apple.com

정의 : Sorts the collection in place. (컬렉션을 제자리에서 정렬합니다.)

- 기본적으로 오름차순으로 정렬함.

- 리턴 값이 없음.

 

예시

var nums : [Int] = [2, 3, 1, 7, 4, 5, 6, 8]
var animals : [String] = ["lion", "dog", "cat", "tigger"]

nums.sort() // [1, 2, 3, 4, 5, 6, 7, 8]
animals.sort() // ["cat", "dog", "lion", "tigger"]

print(nums) // [1, 2, 3, 4, 5, 6, 7, 8]
print(animals) // ["cat", "dog", "lion", "tigger"]

 


sort(by: )

공식문서 : https://developer.apple.com/documentation/swift/array/sort(by:)

 

sort(by:) | Apple Developer Documentation

Sorts the collection in place, using the given predicate as the comparison between elements.

developer.apple.com

정의 : Sorts the collection in place, using the given predicate as the comparison between elements. (지정된 조건자를 요소 간의 비교로 사용하여 컬렉션을 제자리에 정렬합니다.)

- by: 인자 뒤에 조건에 따라 두 원소를 비교하여 제자리 정렬함.

- 원본 배열의 값이 정렬됨.

 

예시

var nums : [Int] = [2, 3, 1, 7, 4, 5, 6, 8]
var animals : [String] = ["lion", "dog", "cat", "tigger"]

nums.sort(by: >) // 내림차순
animals.sort(by: <) // 오름차순

print(nums) // [8, 7, 6, 5, 4, 3, 2, 1]
print(animals) // ["cat", "dog", "lion", "tigger"]

 


sorted()

공식문서 : https://developer.apple.com/documentation/swift/array/sorted()

 

sorted() | Apple Developer Documentation

Returns the elements of the sequence, sorted.

developer.apple.com

정의 : Returns the elemets of the sequence, sorted. (정렬된 시퀀스의 요소를 반환합니다.)

- 원본 배열을 건드리지 않고 정렬한 원소들의 sequence를 리턴함.

- 원본 배열이 그대로 존재함.

 

예시

var nums : [Int] = [2, 3, 1, 7, 4, 5, 6, 8]
var animals : [String] = ["lion", "dog", "cat", "tigger"]

nums.sorted()
animals.sorted()

print(nums) // [2, 3, 1, 7, 4, 5, 6, 8]
print(animals) // ["lion", "dog", "cat", "tigger"]

// 새로운 배열을 생성해 리턴 값을 대입해주어 사용
var numsList = nums.sorted()
print(numsList) // [1, 2, 3, 4, 5, 6, 7, 8]
print(nums) // [2, 3, 1, 7, 4, 5, 6, 8]

// 변수에 대입하여 sorted로 원본 배열 바꾸는법
animals = animals.sorted()
print(animals) // ["cat", "dog", "lion", "tigger"]

 


sorted(by: )

공식문서 : https://developer.apple.com/documentation/swift/array/sorted(by:)

 

sorted(by:) | Apple Developer Documentation

Returns the elements of the sequence, sorted using the given predicate as the comparison between elements.

developer.apple.com

정의 : Return the elements of the sequence, sorted using the given predicate as the comparison between elements. (요소 간의 비교로 주어진 조건자를 사용하여 정렬된 시퀀스의 요소를 반환합니다.)

- by: 인자 뒤에 조건에 따라 두 원소를 비교하여 제자리 정렬함.

- 원본 배열 값을 건드리지 않고 정렬된 시퀀스의 요소를 리턴함.

 

예시

var nums : [Int] = [2, 3, 1, 7, 4, 5, 6, 8]
var animals : [String] = ["lion", "dog", "cat", "tigger"]

nums.sorted(by: >) // 내림차순 : > / 오름차순 : <
animals.sorted(by: >)

print(nums) // [8, 7, 6, 5, 4, 3, 2, 1]
print(animals) // ["tigger", "lion", "dog", "cat"]

 


 

 

sort : 원본배열을 유지하지 않아도 될 때 사용. 리턴 값없음.

sorted : 원본배열을 유지해야 할 때 있을 때 사용. 리턴 값있음.

'Dev > SWIFT' 카테고리의 다른 글

[Swift] Playground  (0) 2023.11.27
[Swift] Swift / iOS 공식자료  (1) 2023.11.26
[Swift] Methods(메소드)  (0) 2023.11.20
[Swift] Properties(프로퍼티)  (0) 2023.11.18
[Swift] Structures and Classes (구조체와 클래스)  (0) 2023.11.17