1 ////////////////////////////////////////////////////////////////////////////
3 // Copyright 2015 Realm Inc.
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
17 ////////////////////////////////////////////////////////////////////////////
23 A `SortDescriptor` stores a key path and a sort order for use with `sorted(sortDescriptors:)`. It is similar to
24 `NSSortDescriptor`, but supports only the subset of functionality which can be efficiently run by Realm's query engine.
26 public struct SortDescriptor {
30 /// The key path which the sort descriptor orders results by.
31 public let keyPath: String
33 /// Whether this descriptor sorts in ascending or descending order.
34 public let ascending: Bool
36 /// Converts the receiver to an `RLMSortDescriptor`.
37 internal var rlmSortDescriptorValue: RLMSortDescriptor {
38 return RLMSortDescriptor(keyPath: keyPath, ascending: ascending)
44 Creates a sort descriptor with the given key path and sort order values.
46 - parameter keyPath: The key path which the sort descriptor orders results by.
47 - parameter ascending: Whether the descriptor sorts in ascending or descending order.
49 public init(keyPath: String, ascending: Bool = true) {
50 self.keyPath = keyPath
51 self.ascending = ascending
56 /// Returns a copy of the sort descriptor with the sort order reversed.
57 public func reversed() -> SortDescriptor {
58 return SortDescriptor(keyPath: keyPath, ascending: !ascending)
62 // MARK: CustomStringConvertible
64 extension SortDescriptor: CustomStringConvertible {
65 /// A human-readable description of the sort descriptor.
66 public var description: String {
67 let direction = ascending ? "ascending" : "descending"
68 return "SortDescriptor(keyPath: \(keyPath), direction: \(direction))"
74 extension SortDescriptor: Equatable {
75 /// Returns whether the two sort descriptors are equal.
76 public static func == (lhs: SortDescriptor, rhs: SortDescriptor) -> Bool {
77 return lhs.keyPath == rhs.keyPath &&
78 lhs.ascending == lhs.ascending
82 // MARK: StringLiteralConvertible
84 extension SortDescriptor: ExpressibleByStringLiteral {
86 public typealias UnicodeScalarLiteralType = StringLiteralType
87 public typealias ExtendedGraphemeClusterLiteralType = StringLiteralType
90 Creates a `SortDescriptor` out of a Unicode scalar literal.
92 - parameter unicodeScalarLiteral: Property name literal.
94 public init(unicodeScalarLiteral value: UnicodeScalarLiteralType) {
95 self.init(keyPath: value)
99 Creates a `SortDescriptor` out of a character literal.
101 - parameter extendedGraphemeClusterLiteral: Property name literal.
103 public init(extendedGraphemeClusterLiteral value: ExtendedGraphemeClusterLiteralType) {
104 self.init(keyPath: value)
108 Creates a `SortDescriptor` out of a string literal.
110 - parameter stringLiteral: Property name literal.
112 public init(stringLiteral value: StringLiteralType) {
113 self.init(keyPath: value)
119 extension SortDescriptor {
120 @available(*, unavailable, renamed: "init(keyPath:ascending:)")
121 public init(property: String, ascending: Bool = true) { fatalError() }
123 @available(*, unavailable, renamed: "keyPath")
124 public var property: String { fatalError() }