<!--
{
  "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/NSSortDescriptor/compare(_:to:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSSortDescriptor(im)compareObject:toObject:"
  },
  "title" : "compare(_:to:)"
}
-->

# compare(_:to:)

Returns a comparison result value that indicates the sort order of two objects.

```
func compare(_ object1: Any, to object2: Any) -> ComparisonResult
```

## Parameters

`object1`

The object to compare with `object2`. This object must have a property accessible using the key-path specified by [`key`](/documentation/Foundation/NSSortDescriptor/key).

`object2`

The object to compare with `object1`. This object must have a property accessible using the key-path specified by [`key`](/documentation/Foundation/NSSortDescriptor/key).

## Return Value

[`ComparisonResult.orderedAscending`](/documentation/Foundation/ComparisonResult/orderedAscending) if `object1` is less than `object2`, [`ComparisonResult.orderedDescending`](/documentation/Foundation/ComparisonResult/orderedDescending) if `object1` is greater than `object2`, or [`ComparisonResult.orderedSame`](/documentation/Foundation/ComparisonResult/orderedSame) if `object1` is equal to `object2`.

## Discussion

The ordering is determined by comparing the values specified by [`key`](/documentation/Foundation/NSSortDescriptor/key) of `object1` and `object2` using the selector specified by [`selector`](/documentation/Foundation/NSSortDescriptor/selector).

---

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)