Sorting and Searching
sorted returns a new array and sort() sorts in place. Use sorted(by:) for custom order and first(where:) or contains(where:) to search.
Example
var n = [5, 2, 9, 1]
print(n.sorted(), n.sorted(by: >))
n.sort()
print(n, n.first(where: { $0 > 4 }) ?? -1)Exercise
Sort in place.
n.()
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.