<!--
{
  "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/NSDictionary/keysSortedByValue(using:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSDictionary(im)keysSortedByValueUsingSelector:"
  },
  "title" : "keysSortedByValue(using:)"
}
-->

# keysSortedByValue(using:)

Returns an array of the dictionary’s keys, in the order they would be in if the dictionary were sorted by its values.

```
func keysSortedByValue(using comparator: Selector) -> [Any]
```

## Parameters

`comparator`

A selector that specifies the method to use to compare the values in the dictionary.

    The     `comparator`     method should return     `NSOrderedAscending`     if the dictionary value is smaller than the argument,     `NSOrderedDescending`     if the dictionary value is larger than the argument, and     `NSOrderedSame`     if they are equal.

## Return Value

An array of the dictionary’s keys, in the order they would be in if the dictionary were sorted by its values.

## Discussion

Pairs of dictionary values are compared using the comparison method specified by `comparator`; the `comparator` message is sent to one of the values and has as its single argument the other value from the dictionary.

## See Also

[`allKeys`](/documentation/Foundation/NSDictionary/allKeys)

A new array containing the dictionary’s keys, or an empty array if the dictionary has no entries.

[`-  sortedArrayUsingSelector:`](/documentation/Foundation/NSArray/sortedArray(using:)-9nhh9)

Returns an array that lists the receiving array’s elements in ascending order, as determined by the comparison method specified by a given 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)