Swift Integer Array – Sort in Decreasing Order

To sort an integer array in decreasing/descending order in Swift, call sort(by:) method on this array and pass > for by parameter. sort(by:) method sorts this array in place based on the condition/predicate passed for by parameter.

Example

In the following program, we will take an array of integers and sort them in decreasing order using sort(by:) method.

main.swift

var arr = [2, 8, 1, 9, 4, 7]
arr.sort(by: >)
print(arr)

Output

Swift Integer Array - Sort in Decreasing Order
ADVERTISEMENT

Conclusion

In this Swift Tutorial, we learned how to sort an Integer Array in decreasing order using sort(by:) method.