首页 > 解决方案 > angularfire2按子值排序列表

问题描述

我有这样的firebase db

    "students" : {
        "-LDD0MIigieZQ3Fcaj1Z" : {
          "ContactNo" : "0752155466",
          "Email" : "sdsdsd@sdsd.com",
          "FirstName" : "Ishana",
          "Gender" : "Female",
          "LastName" : "Dahanayake",
          "Password" : "1234",
          "score" : 6
        },
        "-LDD0ceFiy2RI1avfMWA" : {
          "ContactNo" : "0752155466",
          "Email" : "232323@sdsd.com",
          "FirstName" : "Ruwan",
          "Gender" : "Male",
          "LastName" : "Perera",
          "Password" : "1234",
          "score" : 1
        }
      }

我需要按分数对这些学生进行排序

我试过这样

    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;

但它不会根据分数值改变顺序。我怎样才能对这些值进行排序?

标签: angularsortingfirebasefirebase-realtime-databaseangularfire

解决方案


如果你写的这段代码:

    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;

返回您需要排序的值,我只需添加 return ref.sort();


推荐阅读