Java 如何将以下内容从最高级别排序到最低级别

Java 如何将以下内容从最高级别排序到最低级别,java,firebase-realtime-database,Java,Firebase Realtime Database,我试图了解如何使用Collections.sort和Collections.reverse,并认为我几乎拥有了它,但有时,我的排名没有按照从最高到最低的顺序排列。。。我已经做了一系列的if语句,现在确定这是否是最好的方法,但是有一些奇怪的排序顺序问题。我试图编辑我的数据库,但它没有正确排序 DatabaseReference=FirebaseDatabase.getInstance(“https://medical-review-in-australia.firebaseio.com/") .g

我试图了解如何使用Collections.sort和Collections.reverse,并认为我几乎拥有了它,但有时,我的排名没有按照从最高到最低的顺序排列。。。我已经做了一系列的if语句,现在确定这是否是最好的方法,但是有一些奇怪的排序顺序问题。我试图编辑我的数据库,但它没有正确排序

DatabaseReference=FirebaseDatabase.getInstance(“https://medical-review-in-australia.firebaseio.com/")
.getReference()
.儿童(“诊所”)
.儿童(“诊所”);//getReference()是根//可以继续添加父级

addValueEventListener(新的ValueEventListener(){
@SuppressLint(“SetTextI18n”)
@RequiresApi(api=Build.VERSION\u code.N)
@凌驾
public void onDataChange(@NonNull DataSnapshot DataSnapshot){
list.clear();
popular_clinic_ranking=新ArrayList();
对于(DataSnapshot快照:DataSnapshot.getChildren()){
popular_clinics=String.valueOf(snapshot.child(“ranking”).child(“ranking”).getValue();
if(大众诊所等于(“[超级侦探]”){
popular___name=String.valueOf(snapshot.child(“邮政信息”).child(“名称”).getValue());
popular_clinics_score=snapshot.child(“邮政信息”).child(“总分”).getValue(Integer.class);
}if(大众诊所等于(“[侦探大师]”){
popular___name=String.valueOf(snapshot.child(“邮政信息”).child(“名称”).getValue());
popular_clinics_score=snapshot.child(“邮政信息”).child(“总分”).getValue(Integer.class);
}if(大众诊所等于(“[高级侦探]”){
popular___name=String.valueOf(snapshot.child(“邮政信息”).child(“名称”).getValue());
popular_clinics_score=snapshot.child(“邮政信息”).child(“总分”).getValue(Integer.class);
字符串txt;
txt=“医疗诊所:“+大众诊所\名称+”\n\n总分:“+大众诊所\评分+”\n\n排名:“+大众诊所”;
list.add(txt);
集合。排序(列表);
收款。反向(列表);
adapter.notifyDataSetChanged();//需要同时使用Collections.sort和Collections.reverse。。。

现在我正试图按排名的顺序存储信息,它似乎是这样工作的,但并不是所有的排名都被插入到数据库中…还有,为什么当我有三个父母在两个父母之下的诊所时,计数返回4?是将我的父母中的一个算为孩子吗?在创建时,我是否使用大写字母或小写字母重要我意识到孩子必须小写才能像孩子一样工作

 DatabaseReference reference = FirebaseDatabase.getInstance("https://medical-review-in-australia.firebaseio.com/").getReference().child("Medical Clinics").child("Clinics"); // getReference() is the root  // can keep adding parents
        reference.addValueEventListener(new ValueEventListener() {
            @SuppressLint("SetTextI18n")
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                list.clear();


                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {

                    popular_clinics = String.valueOf(snapshot.child("ranking").child("ranking").getValue());
                    count = (int) snapshot.getChildrenCount() - 1;
                   
                    for(int i = 0; i<= count; i++) {

                        if (popular_clinics.equals("[Super Sleuth]")) {

                            popular_clinics_name = String.valueOf(snapshot.child("Postal Information").child("name").getValue());
                            popular_clinics_score = snapshot.child("Postal Information").child("total_Score").getValue(Integer.class);


                            HashMap<String, Object> map = new HashMap<>();
                            map.put("name", popular_clinics_name);
                            map.put("score", popular_clinics_score);
                            map.put("ranking", popular_clinics);

                            FirebaseDatabase.getInstance("https://medical-review-in-australia.firebaseio.com/").getReference().child("Medical Clinics").child("Popular Clinics").child("Super Sleuth").child("super sleuth").setValue(map);


                        }
DatabaseReference=FirebaseDatabase.getInstance(“https://medical-review-in-australia.firebaseio.com/“”.getReference().child(“医疗诊所”).child(“诊所”);//getReference()是根//可以继续添加父/母
addValueEventListener(新的ValueEventListener(){
@SuppressLint(“SetTextI18n”)
@RequiresApi(api=Build.VERSION\u code.N)
@凌驾
public void onDataChange(@NonNull DataSnapshot DataSnapshot){
list.clear();
对于(DataSnapshot快照:DataSnapshot.getChildren()){
popular_clinics=String.valueOf(snapshot.child(“ranking”).child(“ranking”).getValue();
count=(int)snapshot.getChildrenCount()-1;

对于(int i=0;i而言,问题在于您试图对数字字符串值进行排序。java在这方面有一个愚蠢的问题,例如,如果您的列表中有这些值:

“项目01” “项目02” “项目03” “项目10”

排序后,顺序如下所示:

peopleInfo.sort(Comparator.comparing(info -> info.name));
“项目01” “项目10” “项目02” “项目03”

因此,一种干净的方法是将数据存储在一个类中,例如:

public static class People {
    String name;
    int score;
    String ranking;

    public People(String name, int score, String ranking) {
        this.name = name;
        this.score = score;
        this.ranking = ranking;
    }
}
然后做一个这样的人列表:

ArrayList<People> peopleInfo = new ArrayList<People>();

还请注意,这种排序是区分大小写的。

您使用的是什么语言您好!我使用的是Java。.我使用的是listview,并且读到我必须使用recycledview,这是正确的吗?但是我的整数不是字符串,而是根据我的arraylist排序的,arraylist是一个字符串…分数应该不重要,但我的检查员应该d高于Gumshoetring txt;txt=“医疗诊所:+大众诊所\名称+”\n\n总分:“+大众诊所\评分+”\n\n排名:这是你如何增加你的价值观,我看到了流行的临床评分在中间,这意味着有一个数字转化为StrugSO,我猜这是不可能做到的。我不会继续下去,IITT可以做,我给了你答案的链接。你检查了吗?我刚刚编辑了我的答案和O。这是一个简单的解决方案,看看吧。