I assume the getParticipants() method returns an Observable<List<EventPartcipant>> .
In the snippet below, I first convert Observable<List<EventPartcipant>> to Observable<EventParticipant> using the flatMap() operator. This observable will emit each EventParticipant one at a time. Now, using the toSortedList() operator, I can act on two EventParticipant at the same time, as expected, Comparator<EventParticipant>::compareTo .
In other solutions, sort operators are applied after .observeOn(AndroidSchedulers.mainThread()) , which means that the actual sort occurs in the user interface thread.
I changed this so that the API call is executed in the I / O scheduler, sorted in the calculation scheduler, and finally your sorted list in the user interface thread.
getParticipants().flatMap(new Func1<List<EventParticipant>, Observable<EventParticipant>>() { @Override public Observable<EventParticipant> call(List<EventParticipant> eventParticipants) { return Observable.from(eventParticipants); } }).subscribeOn(Schedulers.io()) .observeOn(Schedulers.computation()) .toSortedList(new Func2<EventParticipant, EventParticipant, Integer>() { @Override public Integer call(EventParticipant eventParticipant, EventParticipant eventParticipant2) { return new StatusComparator().compare(eventParticipant, eventParticipant2); } }).observeOn(AndroidSchedulers.mainThread()) .subscribe(new Action1<List<EventParticipant>>() { @Override public void call(List<EventParticipant> eventParticipants) { // Your sorted list on UI thread. } });
Arun kumar
source share