Alarm+byPriorityThenSpec.swift 929 B

1234567891011121314151617181920212223242526272829303132
  1. // LoopFollow
  2. // Alarm+byPriorityThenSpec.swift
  3. // Created by Jonas Björkert.
  4. import Foundation
  5. extension Alarm {
  6. /// Sorts by `AlarmType.priority`, then the per-type `sortSpec` if one exists.
  7. static let byPriorityThenSpec: (Alarm, Alarm) -> Bool = { lhs, rhs in
  8. // 1) type-level priority
  9. if lhs.type.priority != rhs.type.priority {
  10. return lhs.type.priority < rhs.type.priority
  11. }
  12. // 2) per-type “main value” ordering
  13. if lhs.type == rhs.type,
  14. let spec = lhs.type.sortSpec
  15. {
  16. let lv = spec.key(lhs)
  17. let rv = spec.key(rhs)
  18. switch spec.direction {
  19. case .ascending: return (lv ?? .infinity) < (rv ?? .infinity)
  20. case .descending: return (lv ?? -.infinity) > (rv ?? -.infinity)
  21. }
  22. }
  23. // 3) fallback – keep original insertion order
  24. return false
  25. }
  26. }