[英]Sorting an array of Observables based on an Object value
I am returning an array of Observables from firestore, and want to order them by the index I created within the object in the database. 我正在从Firestore返回一个Observables数组,并希望通过我在数据库对象内创建的索引对它们进行排序。
They are stored in the database like so: 它们像这样存储在数据库中:
They then display in the UI in the order based on the key ID (Alphabetically), however, I want them ordered in the UI based on the index. 然后,它们将根据键ID的顺序显示在UI中(按字母顺序),但是,我希望它们根据索引在UI中进行排序。 I do, however, need to keep the id as the initial key.
但是,我确实需要将id保留为初始密钥。
I have tried mapping the values and using sort() to to order by index shown in the ode below, but still returns in random order. 我尝试过将值映射并使用sort()来按下面的ode中所示的索引排序,但仍然以随机顺序返回。
This is how I have used map to try and order by the key value of index. 这就是我使用map尝试按索引键值进行排序的方式。
this.bedrooms$ = this.property$.pipe(
flatMap((property: any) => this._property.getAllBedrooms(property.bedrooms, this.propertyId))
);
this.sortedBedroom$ = this.bedrooms$.pipe(
map((room: any) => {
return room.sort((a: any, b: any) =>
b.index - a.index
);
}
)
);
Your issue is maybe related to this line b.test - a.index
instead of b.index - a.index
. 您的问题可能与这行
b.test - a.index
而不是b.index - a.index
。
I would suggest another solution for this, getting data sorted from the firestore using AngularFirestore
from @angular/fire
: 我会为此建议另一个解决方案,使用
@angular/fire
AngularFirestore
从firestore中排序数据:
this.afs.collection<Bedroom>('bedrooms', ref => ref.orderBy('index'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.