最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - sort array in RxJS - Stack Overflow

programmeradmin4浏览0评论

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?

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?

Share Improve this question edited Feb 8, 2019 at 10:53 Saleh Mahmood 2,0631 gold badge24 silver badges30 bronze badges asked Oct 22, 2015 at 9:55 GiorgioGiorgio 13.5k13 gold badges50 silver badges75 bronze badges 2
  • 2 The same idea: source.toArray().map(x => x.sort()) Take a look jsbin.com/leqede/edit?js,console – xgrommx Commented Oct 22, 2015 at 15:17
  • I want to get an observable stream of swapping operations. – user663031 Commented Dec 18, 2016 at 5:42
Add a comment  | 

2 Answers 2

Reset to default 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))

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]

发布评论

评论列表(0)

  1. 暂无评论