I am making a chat application and want to arrange user in users list from firebaseStore in alphabetical order. if new users register with the application, these users will be added to the list automatically and arrange in alphabetical order ( like A to B to C, ..). I try to search the answer but I don't find the solution to my issue. Can anyone help me with how to solve it?
Here is my java code in Userfragment.java:
recyclerView = view.findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
mUsers = new ArrayList<>();
imageProfile = view.findViewById(R.id.profile_image);
username = view.findViewById(R.id.username);
storageReference = FirebaseStorage.getInstance().getReference("uploads");
fuser = FirebaseAuth.getInstance().getCurrentUser();
Query query = FirebaseDatabase.getInstance().getReference("Users").child(fuser.getUid()).orderByChild("username");
Collections.sort(mUsers, new Comparator<User>() {
@Override
public int compare(User lhs, User rhs) {
return lhs.getUsername().compareTo(rhs.getUsername());
}
});
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (isAdded()) {
User user = dataSnapshot.getValue(User.class);
username.setText(user.getUsername());
if (user.getImageUrl().equals("default")) {
imageProfile.setImageResource(R.mipmap.ic_launcher);
} else {
Glide.with(getContext()).load(user.getImageUrl()).into(imageProfile);
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
below is some users in realtime database which I want to arrange:
Users 7HMH8nj6AshgYtvD2IMT9a7y2273
id "7HMH8nj6AshgYtvD2IMT9a7y2273" imageUrl "default" search "tony" status "online" username "Tony" AY3gY1eQE1fWJiNUsxxRqpXHBFf2
id "AY3gY1eQE1fWJiNUsxxRqpXHBFf2" imageUrl "default" search "hola" status "offline" username "hola" O6gRe8muq4TGDjQsYkn9Cdpufdy1
id "O6gRe8muq4TGDjQsYkn9Cdpufdy1" imageUrl "default" search "chon" status "offline" username "chon" SGQwWbIZ2vhkPcKqjMuejusKRY13
id "SGQwWbIZ2vhkPcKqjMuejusKRY13" imageUrl "default" search "miko" status "offline" username "miko"