0

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

0

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

2 Comments

I am getting back property 'finished' does not exists on type '{}'.
try (a as any).finished - (b as any).finished
1

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!

2 Comments

I took out time to help you with your problem and you changed the accepted answer. very uncool!!
Sorry if there is misunderstanding, I changed vote from my post to your post today and marked it as accepted answer because you helped me and it was your idea that made app working. I am not sure why it didn't work the first time so i just now checked your post again.
1

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

Comments

0

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

I hope this will work

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.