如何按降序对映射项的键值进行排序? [英] how to sort the key value of a map entry in descending order?

查看:43
本文介绍了如何按降序对映射项的键值进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Java还是比较陌生,使用比较器进行的整个排序对我来说有点混乱.

I am relatively new to Java, and the whole sort by using comparators is a bit confusing to me.

是否可以在不使用比较器的情况下按降序对下面代码的当前输出进行排序?

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);
}

此代码的当前输出为:

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

数据库结构

推荐答案

我能想到的最简单的解决方案是在每个用户对象下添加步骤总数作为新属性.在这种情况下,您还需要做另一件事,那就是使用每天获得的步骤数来增加该属性.您的新架构应如下所示:

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

要根据 totalNumberOfSteps 属性以升序排列用户,请使用以下代码行:

To get the users in (ascending) order according to the totalNumberOfSteps property, please use the following lines of code:

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:

最后,logcat中的结果将是:

Finally, the result in the logcat will be:

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

这篇关于如何按降序对映射项的键值进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆