---
title: "compare(_:to:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/nssortdescriptor/compare(_:to:)"
---

# compare(_:to:)

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

## Declaration

```swift
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 doc://com.apple.foundation/documentation/Foundation/NSSortDescriptor/key.
- `object2`: The object to compare with object1. This object must have a property accessible using the key-path specified by doc://com.apple.foundation/documentation/Foundation/NSSortDescriptor/key.

## Return Value

Return Value ComparisonResult.orderedAscending if object1 is less than object2, ComparisonResult.orderedDescending if object1 is greater than object2, or ComparisonResult.orderedSame if object1 is equal to object2.

## Discussion

Discussion The ordering is determined by comparing the values specified by key of object1 and object2 using the selector specified by selector.

## See Also

### Using Sort Descriptors

- [reversedSortDescriptor](foundation/nssortdescriptor/reversedsortdescriptor.md)
- [allowEvaluation()](foundation/nssortdescriptor/allowevaluation().md)
