josip
josip

Reputation: 167

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" enter image description here

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

Upvotes: 0

Views: 1628

Answers (4)

Ajay K
Ajay K

Reputation: 1

this.db.list('/avatars/').valueChanges().subscribe(d => { this.avatarhall = d.reverse() });

I hope this will work

Upvotes: 0

Rahul
Rahul

Reputation: 1163

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

Upvotes: 1

josip
josip

Reputation: 167

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!

Upvotes: 1

alt255
alt255

Reputation: 3576

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

Upvotes: 1

Related Questions