'Sorting list array from firebase database
again I need a bit of help from you people:-)!
I want to sort data in *ngFor by value "finished"

I get my data with :
this.db.list(`/avatars/`).valueChanges().subscribe(d => {
this.avatarhall = d;
and show it in html like this:
<div *ngFor="let to of avatarhall; let i = index">
<ion-list>
<ion-item>
<ion-thumbnail item-start>
<img src="../assets/ghosts/{{to.avatar}}">
</ion-thumbnail>
<h1> {{to.monstername}}</h1>
<p>Finished Tasks:{{to.finished}}</p>
<ion-icon name="star" item-end>{{i +1}}</ion-icon>
</ion-item>
</ion-list>
</div>
My pipe is created but I don't know how to sort it by the value of finished:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'orderbytask',
})
export class OrderbytaskPipe implements PipeTransform {
transform(value: any[], args:string):any[] {
return value.sort();
}
}
In real, this is a part of app where you can check your position on the ladder
Solution 1:[1]
You can use javascript built in sort function.
this.db.list(`/avatars/`).valueChanges().subscribe(d => {
this.avatarhall = d.sort((a,b) => a.finished - b.finished);
You can find documentation of sort at mdn
Solution 2:[2]
I made it by doing :
this.db.list(`/avatars/`).valueChanges().subscribe(d => {
this.avatarhall = d.sort((a,b) => (a as any).finished - (b as any).finished).reverse();
});
hope someone will find this to be useful. Also, thanks @alt255 for link and starting idea that helped me to figure it out!
Solution 3:[3]
while working with Angular fire version 5.4.2 and Angular 8 project, I had to use below syntax. Need to pass parameter as QueryFn -
CategoryService.ts
getCategories():AngularFireList<any[]>{
return this.afDatabase.list('/categories', ref=>ref.orderByChild('name'));
}
and while calling getCategories method inside component -
Component.ts
constructor(private categoryService: CategoryService) {
categoryService.getCategories().valueChanges().subscribe(items => {
this.categories = items;
});
}
Hope! it will work. Thanks
Solution 4:[4]
this.db.list('/avatars/').valueChanges().subscribe(d => { this.avatarhall = d.reverse() });
I hope this will work
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | alt255 |
| Solution 2 | |
| Solution 3 | Rahul |
| Solution 4 | Akber Iqbal |
