goodsamaritan
goodsamaritan

Reputation: 41

RxSwift Observable Array Sorting

I am trying to sort an observable array and havent had any luck (RxSwift n00b)

let items = [AnyObject]? 
let locations = Observable.just(items)

I want to achieve something like this on locations

items.sortInPlace({$0.name < $1.name})

Any pointers will be appreciated!

Upvotes: 3

Views: 4977

Answers (3)

Pablo Blanco
Pablo Blanco

Reputation: 739

(Swift 4) This is working for me where datasource is a Variable array

var datasource = Variable<[String]>.init([])

_ = self.datasource.value.sort(by: {$0.name < $1.name})

Hope it helps

Upvotes: 0

douarbou
douarbou

Reputation: 2283

here my solution

Observable
   .just(items)
   .map({ (items) -> [AnyObject] in
       return items.sorted(by: { (item1, item2) -> Bool in
          return item1.name < item2.name
       })
   })
   ....

Upvotes: 7

goodsamaritan
goodsamaritan

Reputation: 41

I figured

  1. initing locations as Variable([AnyObject]())
  2. setting locations asObservable
  3. sorting as locations.value.sortInPlace({$0.name < $1.name})

Feels good!

Upvotes: 1

Related Questions