Android:将存储在firebase数据库中的时间按升序排序

问题描述:

嘿,我想将存储在Firebase数据库中的时间以字符串的升序排序并显示在列表中.我发现了有关对整数值进行排序的解决方案.

Hey I would like to sort time stored in firebase database as string in the ascending order and display it in a list. I have found solutions regarding sorting integer values.

这是我的数据库,我想根据 timeTaken 通过该特定问题ID的每个用户ID进行排序. 所以我的问题是如何从那个问题ID (此处:982906)花费的特定时间的每个用户uid中检索数据,并对列表进行排序以检索谁在最短的时间内解决了问题

This is my database where I would like to sort according to the timeTaken by each user id at that specific question id. So my question is how can I retrieve the data from the each users uid of that specific time taken on that question id (here:982906) and sort that list to retrieve who solved the problem in the least time.

private void getTimeTaken() {

    DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("users");
    Query query = databaseReference.orderByChild("questions").startAt("982906");
    query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            yourTimeArraylist = new ArrayList<>();
            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                UserPuzzleDetails userPuzzleDetails = postSnapshot.getValue(UserPuzzleDetails.class);
                yourTimeArraylist.add(new UserPuzzleDetails(userPuzzleDetails.getYourAnswer(), userPuzzleDetails.getAnswerScore(), userPuzzleDetails.getTimeTaken()));
                Log.e("", " " + userPuzzleDetails.getTimeTaken());
            }

            for (int i = 0; i < yourTimeArraylist.size(); i++) {
                UserPuzzleDetails user = yourTimeArraylist.get(i);
                Log.d("test your rank", " onDataChange: " + user.getTimeTaken() + " " + user.getAnswerScore());

            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

我已将数据库中的时间值更改为int(以毫秒为单位),但无法理解如何根据所花费的时间对问题ID 982906的值进行排序.我已经尝试了上述方法,每次都会返回0.

I have changed the values of time in the database as int in milliseconds but am unable to understand how to sort the values of question id 982906 acording to the time taken . I have tried the above method which is returning 0 everytime.

Firebase数据库根据您指定的单个属性(或其他条件),在您查询的位置立即过滤子级.查询不能基于子节点下的动态路径.

A Firebase Database filters the children immediately under the location that you query, based on a single property (or other condition) that you specify. The query cannot be based on a dynamic path under the child nodes.

您正在以不允许该查询的方式嵌套数据:每个答案在树中更深处是一个动态级别:`/users/$ uid/questions/$ qid.这是许多原因之一为什么Firebase文档建议不要以这种方式嵌套数据.

You're nesting data in a way that doesn't allow this query: each answer is one dynamic level deeper in the tree: `/users/$uid/questions/$qid". This is one of the many reasons why the Firebase documentation recommends against nesting data in this way.

如果要按所有用户的花费时间"查询所有用户的答案列表,则应将其准确地存储在数据库中:一个答案列表,每个答案及其花费时间".

If you want to query a list of answers across all users by their "time taken", you should store precisely that in your database: a list of answers, each with their "time taken".

questionAnswers
  $questionId
    $uid
      timeTaken: "08:17:23.6"

现在,您可以通过以下方式获得第42个问题的10个最快答案:

Now you can get the 10 fastest answer for question 42 with:

DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
DatabaseReference answersReference = databaseReference.child("42");
Query query = answersReference.orderByChild("timeTaken");
query.addValueEventListener(new ValueEventListener() {
  @Override
  public void onDataChange(DataSnapshot dataSnapshot) {
    for (DataSnapshot userAnswerSnapshot: dataSnapshot.getChildren()) {
      System.out.println("User "+userAnswerSnapshot.getKey()+" took "+userAnswerSnapshot.child("timeTaken").getValue());
    }
  }