Angular 4 Firebase angularfire 按值对列表进行排序

Angular 4 Firebase angularfire Sorting a list by value

我正在使用 Angular 4 Firebase 和 AngularFire,我有以下 firebase 数据库

"users" : {
    "Test1" : {
      "totalscore" : 50,
      "username" : "test1"
    },
    "Test2" : {
      "totalscore" : 30,
      "username" : "test2"
    },
    "Test3" : {
      "totalscore" : 20,
      "username" : "test1"
    },
    "Test4" : {
      "totalscore" : 10,
      "username" : "test1"
    },
    "Test5" : {
      "totalscore" : 50,
      "username" : "test1"
    },
    "Test6" : {
      "totalscore" : 30,
      "username" : "test2"
    },
    "Test7" : {
      "totalscore" : 20,
      "username" : "test1"
    },
    "Test8" : {
      "totalscore" : 10,
      "username" : "test1"
    }
  }

我阅读了 firebase 和 AngularFire 的文档,我想获取这些数据 作为 totalscore.I 的排序列表,尝试了此 AngularFire link 给出的所有可能方法,但目前 helped.I 没有任何代码可以正常工作,但它没有对列表进行排序。

import { Component, OnInit } from '@angular/core';
import { AngularFireDatabase, FirebaseListObservable } from 'angularfire2/database';

@Component({
  selector: 'app-homefiller',
  templateUrl: './homefiller.component.html',
  styleUrls: ['./homefiller.component.css']
})

export class HomefillerComponent implements OnInit {
  topusers: FirebaseListObservable<any>;
  totalscore;
  list;
  constructor(db: AngularFireDatabase) {


        this.topusers = db.list('users', {
        query: {
         orderByValue: true,
         limitToFirst: 10,
        }
        });
   }

  ngOnInit() {
  }

}

你能帮我想办法按

对我的列表进行排序吗

"totalscore:"

或者告诉我是否可以这样做?? ?

当你有这样的数据结构时使用orderByValue

"userscores" : {
    "Test1" : 50,
    "Test2" : 30,
    "Test3" : 20
    "Test4" : 10,
}

在上面,您希望根据每个子节点的值对 userscores 上的查询结果进行排序。

在您的例子中,您要排序的值在 属性 totalscore 下的子项 users 中。所以你应该使用 orderByChild:

this.topusers = db.list('users', {
    query: {
     orderByChild: "totalscore",
     limitToFirst: 10,
    }
});