sort array in RxJS
Asked Answered
M

2

12

RxJava has a method toSortedList(Comparator comparator) that converts a flow of objects into a list of objects sorted by a Comparator.

How can I achieve the same in JavaScript with RxJS and get an Observable with a flow of objects to emit a sorted array/list?

Mcsweeney answered 22/10, 2015 at 9:55 Comment(2)
The same idea: source.toArray().map(x => x.sort()) Take a look jsbin.com/leqede/edit?js,consoleBanderilla
I want to get an observable stream of swapping operations.Octonary
C
12

you can use the code following:

Rx.Observable.of(5,8,7,9,1,0,6,6,5).toArray().map(arr=>arr.sort()).subscribe(x=>console.log(x))
Claypool answered 1/6, 2016 at 2:57 Comment(0)
G
3

With [email protected]

import { of } from 'rxjs';
import { map, toArray } from 'rxjs/operators';

const obs = of(5,8,7,9,1,0,6,6,5).pipe(
  toArray(),
  map(arr=> arr.sort((a,b) => a - b)
);


obs.subscribe(x => {
  console.log(x);
});

outputs [0, 1, 5, 5, 6, 6, 7, 8, 9]

Gaultheria answered 27/5, 2021 at 18:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.