首页 > 解决方案 > android studio Arraylist 按降序排序

问题描述

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

预期产量:500 Airene 200 Aiexa 3 Airene 1 Alexander

我以这种方式获取和排序

 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 Airene 3 Airene 200 Aiexa 1 Alexander Arrays.sort(names, Collections.reverseOrder()); 可能不适合我,因为“数字和字母”组合为字符串”? Usersname.add(Cred+" "+friend);

标签: javaandroid

解决方案


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

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]

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


推荐阅读