0

I have a custom listview which fetch data from firebasedatabase ,question is how im i able to sort my arraylist ex. "1 alexander" , "500 Airene" , "3 Airene","200 Aiexa"

Expected output : 500 Airene 200 Aiexa 3 Airene 1 Alexander

Im fetching and sorting it this way

 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) {

                    }
                });


But when using this code my output is :

500 Airene 3 Airene 200 Aiexa 1 Alexander Arrays.sort(names, Collections.reverseOrder()); is not working for me may be, because " number and alphabet" are combine as string"?? Usersname.add(Cred+" "+friend);

1
  • Right, if you use a generic Comparator, it's going to use natural order. It doesn't know the meanings of the fields inside your String. You'd have to write your own Comparator for that, pass it as the second argument to sort(): something like this. Commented Dec 22, 2019 at 23:31

1 Answer 1

0

Here is a custom comparator that meets your need:

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

This outputs: [500 Airene, 200 Aiexa, 3 Airene, 1 alexander]

This will work assuming that all of your strings has the format: "<integer> <letters>"

Sign up to request clarification or add additional context in comments.

8 Comments

Hi Christilyn my system printout goes like this I/System.out: [500 Airene, 3 Airene, 200 Aiexa, 1 alexander]
@AiexaAlexander Can you update your post with your new implementation?
it should be 500 Airene,200 Aiexa, 3 Airene,1 alexander
yeheey!! it worked..thank you very much!! ``` I/System.out: [500 Airene, 200 Aiexa, 3 Airene, 1 alexander]```
@AiexaAlexander if it answers your question mark it as correct!!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.