<!--
{
  "availability" : [
    "iOS: 2.0.0 -",
    "iPadOS: 2.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.0.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSArray/sortedArray(using:)-9nhh9",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSArray(im)sortedArrayUsingSelector:"
  },
  "title" : "sortedArray(using:)"
}
-->

# 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.

```
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 `NSOrderedAscending` if the receiving array is smaller than the argument, `NSOrderedDescending` if the receiving array is larger than the argument, and `NSOrderedSame` if 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(_:)`](/documentation/Foundation/NSString/caseInsensitiveCompare(_:)) method declared in the `NSString` class. Assuming `anArray` exists, a sorted version of the array can be created in this way:

```objc
     NSArray *sortedArray =
         [anArray sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
```

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)