[SOLVED] Sorting list array from firebase database

Issue

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

Solution

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

Answered By – alt255

Answer Checked By – Willingham (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *