To sort an array in descending order in Swift, you can use the `sorted(by:)` method with a custom comparison function.
In this example,
numbers
.sorted(by:)
method with a custom comparison function to sort the array of integers in descending order.var numbers = [5, 2, 8, 1, 9]
numbers.sort(by: { $0 > $1 })
print(numbers)
[9, 8, 5, 2, 1]
In this example,
words
.sorted(by:)
method with a custom comparison function to sort the array of strings in descending order.var words = ["apple", "banana", "cherry", "date"]
words.sort(by: { $0 > $1 })
print(words)
["date", "cherry", "banana", "apple"]
In this tutorial, we learned How to Sort an Array in Descending Order in Swift language with well detailed examples.