【问题标题】:android studio Arraylist sort by descending orderandroid studio Arraylist 按降序排序
【发布时间】:2019-12-22 22:37:57
【问题描述】:

我有一个自定义列表视图,它从 firebasedatabase 获取数据,问题是我如何能够对我的 arraylist 前进行排序。 “1 alexander”、“500 Airene”、“3 Airene”、“200 Aiexa”

预期输出: 500 艾琳 200 艾克萨 3 艾琳 1 亚历山大

我是这样获取和排序的

 FirebaseDatabase database = FirebaseDatabase.getInstance();
        database.getReference()
                .child("Users").child("Displayname")
                .addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        for (DataSnapshot snapshot : dataSnapshot.getChildren()){


                            System.out.println(snapshot.getKey()); 

                            Map<String, Object> map =(Map<String, Object>) snapshot.getValue();

                            for (Map.Entry<String, Object> entry : map.entrySet()){

                                System.out.println(entry.getKey()); 

                                System.out.println(entry.getValue().toString()); 
                                Cred = (String) entry.getValue();
                                int numberOfUsers = (int) dataSnapshot.getChildrenCount();


                                UsersNumber.setText("Total Users : "+String.valueOf(numberOfUsers));



                                professions = new String[numberOfUsers];
                                Prof.add(Cred);
                                professions = new String[Prof.size()];
                                Prof.toArray(professions);

                                friend = snapshot.getKey();
                                names = new String[numberOfUsers];
                                Usersname.add(Cred+"  "+friend);
                                names = new String[Usersname.size()];
                                Usersname.toArray(names);
                                names = new String[Usersname.size()];
                                Usersname.toArray(names);

                                customListView=(ListView)findViewById(R.id.custom_list_view);
                                userInfos=new ArrayList<>();
                                Arrays.sort(names, Collections.reverseOrder());
                                //Arrays.sort(names, String.CASE_INSENSITIVE_ORDER);
                               // Arrays.sort(professions, String.CASE_INSENSITIVE_ORDER);
                                customListAdapter=new CustomListAdapter(userInfos,MainActivityCustonlistViewnew.this);
                                customListView.setAdapter(customListAdapter);
                                getDatas();
                             //   Toast.makeText(MainActivityCustonlistViewnew.this,String.valueOf(numberOfUsers) , Toast.LENGTH_SHORT).show();


                                customListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                    @Override
                                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                                    //    Toast.makeText(MainActivityCustonlistViewnew.this, "Name : " + names[i] + "\n Profession : " + professions[i], Toast.LENGTH_SHORT).show();

                                    }
                                });


                            }



                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });


但是当使用这段代码时,我的输出是:

500 艾琳 3 艾琳 200 艾克萨 1 亚历山大 Arrays.sort(names, Collections.reverseOrder()); 可能对我不起作用,因为“数字和字母”组合为字符串“?? Usersname.add(Cred+" "+friend);

【问题讨论】:

  • 对,如果你使用通用的Comparator,它将使用natural order。它不知道String 中的字段的含义。为此,您必须编写自己的Comparator,将其作为第二个参数传递给sort()something like this

标签: java android


【解决方案1】:

这是一个满足您需求的自定义比较器:

String[] mArray = new String[] {
        "1 alexander",
        "500 Airene",
        "3 Airene",
        "200 Aiexa"
};

Comparator comparator = new Comparator<String>() {
       @Override
            public int compare(String o1, String o2) {
                String num1 = o1.split(" ")[0];
                String num2 = o2.split(" ")[0];
                return Integer.parseInt(num2) - Integer.parseInt(num1);
            }
 };
 Arrays.sort(mArray, comparator);
 System.out.println(Arrays.toString(mArray));

这输出: [500 Airene, 200 Aiexa, 3 Airene, 1 alexander]

假设您的所有字符串都具有以下格式,这将起作用:"&lt;integer&gt; &lt;letters&gt;"

【讨论】:

  • 嗨 Christilyn 我的系统打印输出是这样的I/System.out: [500 Airene, 3 Airene, 200 Aiexa, 1 alexander]
  • @AiexaAlexander 你能用你的新实现更新你的帖子吗?
  • 应该是 500 Airene,200 Aiexa, 3 Airene,1 alexander
  • 耶嘿嘿!!它工作..非常感谢你! ``` I/System.out: [500 Airene, 200 Aiexa, 3 Airene, 1 alexander]```
  • @AiexaAlexander 如果它回答了您的问题,请将其标记为正确!
猜你喜欢
  • 2011-08-12
  • 2011-08-19
  • 2016-09-05
  • 2013-09-10
  • 2012-06-06
  • 2014-10-31
  • 1970-01-01
  • 2012-10-18
  • 1970-01-01
相关资源
最近更新 更多