Angular 2 - 从 Observable 排序列表

Angular 2 - Sort list from Observable

sort 来自 Observable 的项目列表并且仍然能够使用 async pipe 的最佳方法是什么? (我读到制作自定义排序管道并不是很有效。)我想避免订阅和保留数据的本地副本,因此只使用异步管道...

//can I use map here and filter items right in the observable and get rid of subscribe?

this.test$ = Observable.of(['one', 'two', 'three'])
    .subscribe((data) => {
        data.sort((a, b) => {
            return a < b ? -1 : 1;
         });
        this.data = data;
     });

模板:

<div *ngFor='let item of data'>
<!-- want to be able to use async pipe here -->

如果您调用 .subscribe(),您会得到一个 Subscription,异步管道需要一个 Observable

如果你把它改成

this.test$ = Observable.of(['one', 'two', 'three'])
.map((data) => {
    data.sort((a, b) => {
        return a < b ? -1 : 1;
     });
    return data;
 });

您可以将异步管道与 test$

一起使用