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

  • Right, if you use a generic `Comparator`, it's going to use [natural order](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#compareTo(java.lang.String)). 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](https://stackoverflow.com/questions/16541314/sorting-an-array-of-string-with-custom-ordering). – greeble31 Dec 22 '19 at 23:31

1 Answers1

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>"

Christilyn Arjona
  • 2,173
  • 3
  • 13
  • 20