<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 13.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "HealthKit",
  "identifier" : "/documentation/HealthKit/HKQuantity/compare(_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "HealthKit"
    ],
    "preciseIdentifier" : "c:objc(cs)HKQuantity(im)compare:"
  },
  "title" : "compare(_:)"
}
-->

# compare(_:)

Compares two values after converting them to the same units.

```
func compare(_ quantity: HKQuantity) -> ComparisonResult
```

## Parameters

`quantity`

The quantity to compare. This method throws an exception if the quantities do not have compatible units (<doc://com.apple.documentation/documentation/Foundation/NSExceptionName/invalidArgumentException>).

## Return Value

<doc://com.apple.documentation/documentation/Foundation/ComparisonResult/orderedDescending> if the parameter is less than the receiver. <doc://com.apple.documentation/documentation/Foundation/ComparisonResult/orderedAscending> if the parameter is greater than the receiver. <doc://com.apple.documentation/documentation/Foundation/ComparisonResult/orderedSame> if the quantities are equal.

## Discussion

Returns whether the quantity argument is less than, equal to, or greater than the current quantity. This method automatically converts the quantities into the same units before comparing the values. You just need to ensure that the quantities have compatible units.

> Note:
> Converting a value to a different unit can introduce floating point errors. Values that should be equal may appear unequal due to these floating point errors.

In most cases, the compatible units are clear from context. To see the unit types associated with different quantity sample types, see the type identifiers in [`HKQuantityTypeIdentifier`](/documentation/HealthKit/HKQuantityTypeIdentifier).

If you need to programmatically check whether a particular unit is compatible with a particular quantity, call the quantity’s [`is(compatibleWith:)`](/documentation/HealthKit/HKQuantity/is(compatibleWith:)) method.

## See Also

[`-  isCompatibleWithUnit:`](/documentation/HealthKit/HKQuantity/is(compatibleWith:))

Returns a boolean value indicating whether the quantity is compatible with the provided unit.



---

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)