如何根据值< String,Long>对值进行排序在数组列表中? [英] How to sort values from value <String, Long> in an arraylist?

查看:104
本文介绍了如何根据值< String,Long>对值进行排序在数组列表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to sort the current output of the code below in descending order without using comparators?

我尝试过使用它,但是由于总是出现错误,因此我似乎无法使其正常工作.

I've tried using one but I can't seem to make it work as it always gives an error.

我尝试查看这两个链接,因为我知道答案可能在那里: > Sort Map< String,Long> 对地图进行排序<键,值>按值
但是由于我对Java很陌生,所以我并没有真正获得使它正常工作所需的工作.

I've tried looking at these two links as I know the answers might be in there: Sort Map<String, Long> by value reversed , Sort a Map<Key, Value> by values
But since I'm quite new to java I don't really get what I need to do in order to get it working.

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

首选此代码来运行以下输出:

preferred 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

有人可以向我解释我如何需要使用那些所说的链接来使其对我有用,这样我才能获得上面列出的输出?

Is it possible for someone to explain to me how I would need to use those said links to get it working for me so I could get the output as listed above ?

推荐答案

Firebase实时数据库可以返回按每个子节点的属性值排序的子节点.但是,由于您是通过汇总数据库中的值来确定existingSteps的,因此除非您也将汇总值存储在数据库中,否则无法以此顺序从数据库中获取节点.

The Firebase Realtime Database can return child nodes ordered by the value of a property of each child node. But since you determine existingSteps by aggregating values from the database, there is no way to get the node from the database in this order, unless you also store the aggregated value in the database.

以当前数据格式,在汇总所有用户的步骤后,您将需要重新排序阵列列表客户端中的项目.

In your current data format, you will need to re-order the items in your array list client-side after aggregating the steps for all users.

如果您要修改数据格式以包括汇总的existingSteps值,请记住,Firebase只能按升序顺序返回结果.这意味着要以相反的顺序显示它们,您要么必须反转数组列表客户端,要么在数据库中存储一个具有倒置值existingSteps的附加属性,以便对其进行排序就可以得到相反的结果.有关更多信息,请参见 firebase->日期顺序倒退

If you were to modify the data format to include the aggregated existingSteps value, keep in mind that Firebase can only return results in ascending order. That means that to show them in reverse order, you'll either have to reverse the array list client-side, or store an additional property in the database that has the inverted value of existingSteps so that ordering on that gives the reversed results. For more on this, see firebase -> date order reverse

这篇关于如何根据值&lt; String,Long&gt;对值进行排序在数组列表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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