sortedArray(using:)
Returns an array that lists the receiving array’s elements in ascending order, as determined by the comparison method specified by a given selector.
Declaration
func sortedArray(using comparator: Selector) -> [Any]Parameters
- comparator:
A selector that identifies the method to use to compare two elements at a time. The method should return
NSOrderedAscendingif the receiving array is smaller than the argument,NSOrderedDescendingif the receiving array is larger than the argument, andNSOrderedSameif they are equal.
Return Value
An array that lists the receiving array’s elements in ascending order, as determined by the comparison method specified by the selector comparator.
Discussion
The new array contains references to the receiving array’s elements, not copies of them.
The comparator message is sent to each object in the array and has as its single argument another object in the array.
For example, an array of NSString objects can be sorted by using the caseInsensitiveCompare(_:) method declared in the NSString class. Assuming anArray exists, a sorted version of the array can be created in this way:
NSArray *sortedArray =
[anArray sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];