<!--
{
  "availability" : [
    "iOS: 10.0.0 -",
    "iPadOS: 10.0.0 -",
    "macCatalyst: 10.0.0 -",
    "macOS: 10.12.0 -",
    "tvOS: 10.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 3.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/DateInterval/compare(_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "s:10Foundation12DateIntervalV7compareySo18NSComparisonResultVACF"
  },
  "title" : "compare(_:)"
}
-->

# compare(_:)

Compares two intervals.

```
func compare(_ dateInterval: DateInterval) -> ComparisonResult
```

## Discussion

This method prioritizes ordering by start date. If the start dates are equal, then it will order by duration. e.g. Given intervals a and b

```swift
a.   |-----|
b.      |-----|
```

`a.compare(b)` would return `.OrderedAscending` because a’s start date is earlier in time than b’s start date.

In the event that the start dates are equal, the compare method will attempt to order by duration. e.g. Given intervals c and d

```swift
c.  |-----|
d.  |---|
```

`c.compare(d)` would result in `.OrderedDescending` because c is longer than d.

If both the start dates and the durations are equal, then the intervals are considered equal and `.OrderedSame` is returned as the result.

---

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)