Skip to content

Commit 929b33c

Browse files
committed
Make toSortedArray similar to toArray
1 parent 3da5d6f commit 929b33c

File tree

2 files changed

+22
-26
lines changed

2 files changed

+22
-26
lines changed

Source/RxSwift/toSortedArray.swift

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,33 @@
22
// toSortedArray.swift
33
// RxSwiftExt
44
//
5-
// Created by Joan Disho on 17.02.18.
6-
// Copyright © 2018 RxSwiftCommunity. All rights reserved.
5+
// Created by Joan Disho on 17/02/18.
6+
// Copyright © 2018 RxSwift Community. All rights reserved.
77
//
88

99
import Foundation
1010
import RxSwift
1111

12-
extension Observable where E: Sequence, E.Element: Comparable {
12+
public extension ObservableType {
1313
/**
14-
Transforms an observable of comparables into an observable of ordered arrays.
14+
Converts an Observable into another Observable that emits the whole sequence as a single array, sorts it using the provided closure and then terminates.
1515

16-
- parameter ascending: Defined the sorting direction. Defaults to ascending.
17-
- returns: The sorted observable.
16+
- parameter by: A comparator closure to sort emitted elements.
17+
- returns: An observable sequence containing all the sorted emitted elements as an array.
1818
*/
19-
public func toSortedArray<T>(ascending: Bool = true)
20-
-> Observable<[T]> where E.Element == T {
21-
return toSortedArray({ ascending ? $0 < $1 : $0 > $1 })
19+
func toSortedArray(by: @escaping (E, E) -> Bool) -> Observable<[E]> {
20+
return toArray().map { $0.sorted(by: by) }
2221
}
22+
}
2323

24+
public extension ObservableType where E: Comparable {
2425
/**
25-
Transforms an observable of comparables into an observable of ordered arrays by using the function passed in.
26-
27-
- parameter areInIncreasingOrder: A function to compare to elements.
28-
- returns: The sorted observable.
29-
*/
30-
public func toSortedArray<T>(_ areInIncreasingOrder: @escaping (T, T) -> Bool)
31-
-> Observable<[T]> where E.Element == T {
32-
return toArray().map { elements in
33-
elements.flatMap { element in
34-
element.sorted(by: areInIncreasingOrder)
35-
}
36-
}
26+
Converts an Observable into another Observable that emits the whole sequence as a single array, sorts it using the provided closure and then terminates.
27+
28+
- parameter ascending: Should the emitted items be ascendign or descending.
29+
- returns: An observable sequence containing all the sorted emitted elements as an array.
30+
*/
31+
func toSortedArray(ascending: Bool = true) -> Observable<[E]> {
32+
return toSortedArray(by: { ascending ? $0 < $1 : $0 > $1 })
3733
}
3834
}

Tests/RxSwift/ToSortedArrayTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class ToSortedArrayTests: XCTestCase {
2020
}
2121

2222
func testDefaultToSortedArray() {
23-
let source = Observable.of([1, 4, 6, 1, 7, 8])
23+
let source = Observable.of(1, 4, 6, 1, 7, 8)
2424
let observer = runAndObserve(source.toSortedArray())
2525
let correct = [
2626
next(0, [1, 1, 4, 6, 7, 8]),
@@ -30,8 +30,8 @@ class ToSortedArrayTests: XCTestCase {
3030
}
3131

3232
func testAscCase() {
33-
let source = Observable.of([1, 4, 6, 1, 7, 8])
34-
let observer = runAndObserve(source.toSortedArray(<))
33+
let source = Observable.of(1, 4, 6, 1, 7, 8)
34+
let observer = runAndObserve(source.toSortedArray(ascending: true))
3535
let correct = [
3636
next(0, [1, 1, 4, 6, 7, 8]),
3737
completed(0)
@@ -40,8 +40,8 @@ class ToSortedArrayTests: XCTestCase {
4040
}
4141

4242
func testDescCase() {
43-
let source = Observable.of([1, 4, 6, 1, 7, 8])
44-
let observer = runAndObserve(source.toSortedArray(>))
43+
let source = Observable.of(1, 4, 6, 1, 7, 8)
44+
let observer = runAndObserve(source.toSortedArray(ascending: false))
4545
let correct = [
4646
next(0, [8, 7, 6, 4, 1, 1]),
4747
completed(0)

0 commit comments

Comments
 (0)