SortDescriptor.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2015 Realm Inc.
  4. //
  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
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  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.
  16. //
  17. ////////////////////////////////////////////////////////////////////////////
  18. import Foundation
  19. import Realm
  20. /**
  21. A `SortDescriptor` stores a key path and a sort order for use with `sorted(sortDescriptors:)`. It is similar to
  22. `NSSortDescriptor`, but supports only the subset of functionality which can be efficiently run by Realm's query engine.
  23. */
  24. public struct SortDescriptor {
  25. // MARK: Properties
  26. /// The key path which the sort descriptor orders results by.
  27. public let keyPath: String
  28. /// Whether this descriptor sorts in ascending or descending order.
  29. public let ascending: Bool
  30. /// Converts the receiver to an `RLMSortDescriptor`.
  31. internal var rlmSortDescriptorValue: RLMSortDescriptor {
  32. return RLMSortDescriptor(keyPath: keyPath, ascending: ascending)
  33. }
  34. // MARK: Initializers
  35. /**
  36. Creates a sort descriptor with the given key path and sort order values.
  37. - parameter keyPath: The key path which the sort descriptor orders results by.
  38. - parameter ascending: Whether the descriptor sorts in ascending or descending order.
  39. */
  40. public init(keyPath: String, ascending: Bool = true) {
  41. self.keyPath = keyPath
  42. self.ascending = ascending
  43. }
  44. // MARK: Functions
  45. /// Returns a copy of the sort descriptor with the sort order reversed.
  46. public func reversed() -> SortDescriptor {
  47. return SortDescriptor(keyPath: keyPath, ascending: !ascending)
  48. }
  49. }
  50. // MARK: CustomStringConvertible
  51. extension SortDescriptor: CustomStringConvertible {
  52. /// A human-readable description of the sort descriptor.
  53. public var description: String {
  54. let direction = ascending ? "ascending" : "descending"
  55. return "SortDescriptor(keyPath: \(keyPath), direction: \(direction))"
  56. }
  57. }
  58. // MARK: Equatable
  59. extension SortDescriptor: Equatable {
  60. /// Returns whether the two sort descriptors are equal.
  61. public static func == (lhs: SortDescriptor, rhs: SortDescriptor) -> Bool {
  62. return lhs.keyPath == rhs.keyPath &&
  63. lhs.ascending == rhs.ascending
  64. }
  65. }
  66. // MARK: StringLiteralConvertible
  67. extension SortDescriptor: ExpressibleByStringLiteral {
  68. public typealias UnicodeScalarLiteralType = StringLiteralType
  69. public typealias ExtendedGraphemeClusterLiteralType = StringLiteralType
  70. /**
  71. Creates a `SortDescriptor` out of a Unicode scalar literal.
  72. - parameter unicodeScalarLiteral: Property name literal.
  73. */
  74. public init(unicodeScalarLiteral value: UnicodeScalarLiteralType) {
  75. self.init(keyPath: value)
  76. }
  77. /**
  78. Creates a `SortDescriptor` out of a character literal.
  79. - parameter extendedGraphemeClusterLiteral: Property name literal.
  80. */
  81. public init(extendedGraphemeClusterLiteral value: ExtendedGraphemeClusterLiteralType) {
  82. self.init(keyPath: value)
  83. }
  84. /**
  85. Creates a `SortDescriptor` out of a string literal.
  86. - parameter stringLiteral: Property name literal.
  87. */
  88. public init(stringLiteral value: StringLiteralType) {
  89. self.init(keyPath: value)
  90. }
  91. }
  92. // MARK: Unavailable
  93. extension SortDescriptor {
  94. @available(*, unavailable, renamed: "init(keyPath:ascending:)")
  95. public init(property: String, ascending: Bool = true) { fatalError() }
  96. @available(*, unavailable, renamed: "keyPath")
  97. public var property: String { fatalError() }
  98. }