简体   繁体   English

如何按降序对映射项的键值进行排序?

[英]how to sort the key value of a map entry in descending order?

I am relatively new to Java, and the whole sort by using comparators is a bit confusing to me. 我对Java比较陌生,使用比较器进行的整体排序对我来说有点令人困惑。

Is it possible to sort the current output of the code below in descending order without using comparators? 是否可以在不使用比较器的情况下按降序对下面代码的当前输出进行排序?

Or is there an easier way for me to understand how to sort said values. 还是有一种更简单的方法让我理解如何对所述值进行排序。

myRef.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(@NonNull DataSnapshot dataSnapshot , String s) {
        Map<String, Object> userInfo = (Map<String, Object>)dataSnapshot.getValue();
        String username = (String) userInfo.get("username");

        Map<String, Long> steps = (Map<String, Long>) userInfo.get("steps");

        long existingSteps = 0;
        for (Map.Entry<String, Long> step : steps.entrySet()) {
            existingSteps += Long.valueOf(step.getValue());
        }

        arrayList.add(new String( username + "  -  " + "steps:  " + existingSteps));

        arrayAdapter = new ArrayAdapter<>(Leaderboard.this , 
        android.R.layout.simple_list_item_1 , arrayList);

        listView.setAdapter(arrayAdapter);
}

The current output of this code is: 该代码的当前输出为:

John Doe I - Steps: 79
John Doe II - Steps: 111
John Doe III - Steps: 0
John Doe IV - Steps: 88
John Doe V - Steps: 12
John Doe VI - Steps: 0

Is it possible to get this code to run the following output: 是否有可能获得此代码来运行以下输出:

John Doe II - Steps: 111
John Doe IV - Steps: 88
John Doe I - Steps: 79
John Doe V - Steps: 12
John Doe III - Steps: 0
John Doe VI - Steps: 0

Database Structure 数据库结构

The simplest solution I can think of, is to add under each user object, the total number of steps, as a new property. 我能想到的最简单的解决方案是在每个用户对象下添加步骤总数作为新属性。 In this case, there is one more thing that you need to do, which is to increment that property with the number of steps that you get every day. 在这种情况下,您还需要做另一件事,那就是使用每天获得的步骤数来增加该属性。 Your new schema should look like this: 您的新架构应如下所示:

Firebase-root
  |
  --- user
       |
       --- userId
            |
            --- steps
            |    |
            |    --- //daily steps
            |
            --- target: 100
            |
            --- username: "John Doe I"
            |
            --- totalNumberOfSteps: 111

To get the users in (ascending) order according to the totalNumberOfSteps property, please use the following lines of code: 要根据totalNumberOfSteps属性以升序排列用户,请使用以下代码行:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference userRef = rootRef.child("user");
Query query = userRef.orderByChild("totalNumberOfSteps");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String username = ds.child("username").getValue(String.class);
            long totalNumberOfSteps = ds.child("totalNumberOfSteps").getValue(Long.class);
            Log.d(TAG, username + " - Steps: " + totalNumberOfSteps);
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
    }
};
query.addListenerForSingleValueEvent(valueEventListener);

Then to order it descending, please see my answer from the following post: 然后按降序排列,请从以下帖子中查看我的答案:

Finally, the result in the logcat will be: 最后,logcat中的结果将是:

John Doe II - Steps: 111
John Doe IV - Steps: 88
John Doe I - Steps: 79
John Doe V - Steps: 12
John Doe III - Steps: 0
John Doe VI - Steps: 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM