Alarm+byPriorityThenSpec.swift 898 B

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