You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
1.2 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. //
  2. // toSortedArray.swift
  3. // RxSwiftExt
  4. //
  5. // Created by Joan Disho on 17/02/18.
  6. // Copyright © 2018 RxSwift Community. All rights reserved.
  7. //
  8. import Foundation
  9. import RxSwift
  10. public extension ObservableType {
  11. /**
  12. Converts an Observable into another Observable that emits the whole sequence as a single array sorted using the provided closure and then terminates.
  13. - parameter by: A comparator closure to sort emitted elements.
  14. - returns: An observable sequence containing all the sorted emitted elements as an array.
  15. */
  16. func toSortedArray(by: @escaping (Element, Element) -> Bool) -> Single<[Element]> {
  17. return toArray().map { $0.sorted(by: by) }
  18. }
  19. }
  20. public extension ObservableType where Element: Comparable {
  21. /**
  22. Converts an Observable into another Observable that emits the whole sequence as a single sorted array and then terminates.
  23. - parameter ascending: Should the emitted items be ascending or descending.
  24. - returns: An observable sequence containing all the sorted emitted elements as an array.
  25. */
  26. func toSortedArray(ascending: Bool = true) -> Single<[Element]> {
  27. return toSortedArray(by: { ascending ? $0 < $1 : $0 > $1 })
  28. }
  29. }