<!--
{
  "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/NSMutableArray/sort(_:context:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSMutableArray(im)sortUsingFunction:context:"
  },
  "title" : "sort(_:context:)"
}
-->

# sort(_:context:)

Sorts the receiver in ascending order as defined by the comparison function `compare`.

```
func sort(_ compare: (Any, Any, UnsafeMutableRawPointer?) -> Int, context: UnsafeMutableRawPointer?)
```

## Parameters

`compare`

The comparison function to use to compare two elements at a time.

    The function’s parameters are two objects to compare and the context parameter,     `context`    . The function should return     `NSOrderedAscending`     if the first element is smaller than the second,     `NSOrderedDescending`     if the first element is larger than the second, and     `NSOrderedSame`     if the elements are equal.

`context`

The context argument to be passed to the compare function.

## Discussion

This approach allows the comparison to be based on some outside parameter, such as whether character sorting is case sensitive or case insensitive.

## See Also

[`-  sortedArrayUsingFunction:context:`](/documentation/Foundation/NSArray/sortedArray(_:context:))

Returns a new array that lists the receiving array’s elements in ascending order as defined by the comparison function `comparator`.



---

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)