LVW23
LVW23

Reputation: 85

angularfire2 sort list by child value

I have my firebase db like this

    "students" : {
        "-LDD0MIigieZQ3Fcaj1Z" : {
          "ContactNo" : "0752155466",
          "Email" : "[email protected]",
          "FirstName" : "Ishana",
          "Gender" : "Female",
          "LastName" : "Dahanayake",
          "Password" : "1234",
          "score" : 6
        },
        "-LDD0ceFiy2RI1avfMWA" : {
          "ContactNo" : "0752155466",
          "Email" : "[email protected]",
          "FirstName" : "Ruwan",
          "Gender" : "Male",
          "LastName" : "Perera",
          "Password" : "1234",
          "score" : 1
        }
      }

I need to sort these students by score

i tried like this

    let ref = this.afDB.list('/students',ref=>ref.orderByChild('score')).snapshotChanges()
        .map(changes =>{
          return changes.map(c=> ({key:c.payload.key,...c.payload.val()}));
        });
    
        return ref;

but its not changing the order according to the score value.How can i sort these values?

Upvotes: 2

Views: 1671

Answers (1)

josip
josip

Reputation: 167

If this code you wrote :

    let ref = this.afDB.list('/students',ref=>ref.orderByChild('score')).snapshotChanges()
        .map(changes =>{
          return changes.map(c=> ({key:c.payload.key,...c.payload.val()}));
        });
    
        return ref;

returns you values you need to sort, i would just add return ref.sort();

Upvotes: 1

Related Questions