简体   繁体   English

如何从高到低排序以下内容

[英]How to sort the following from highest rank to the lowest

I am trying to understand how to use Collections.sort and Collections.reverse and thought that I almost had it but sometimes, my ranking is not placed in order from the highest to the lowest... I have done a series of if statements and now sure if this is the best way to go about but have some weird sorting order problem.我试图了解如何使用 Collections.sort 和 Collections.reverse 并认为我几乎拥有它但有时,我的排名不是按照从最高到最低的顺序排列的......我已经做了一系列的 if 语句和现在确定这是否是最好的方法,但有一些奇怪的排序问题。 I tried to edit my database but it is not sorting it properly....我试图编辑我的数据库,但它没有正确排序......

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

        popular_clinic_ranking = new ArrayList<>();


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

             popular_clinics = String.valueOf(snapshot.child("ranking").child("ranking").getValue());



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


               
        

             }   if (popular_clinics.equals("[Master Detective]")) {

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

          
          

            }   if (popular_clinics.equals("[Senior Detective]")) {

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


        String txt;
        txt = "Medical Clinic: " + popular_clinics_name + "\n\nTotal Score: " + popular_clinics_score + "\n\nRanking: " + popular_clinics;




            list.add(txt);
            Collections.sort(list);
            Collections.reverse(list);
            adapter.notifyDataSetChanged();  // need to use both Collections.sort and Collections.reveerse...

应用界面

Now I am trying to store the information in the order of ranking and it seems to be working this way but not all the ranking are inserted into the database... Also, why is count returning 4 when I have 3 medical clinics under two parents?现在我正在尝试按排名顺序存储信息,它似乎是这样工作的,但并非所有排名都插入到数据库中......另外,当我在两个父母下有 3 个医疗诊所时,为什么计数返回 4 ? Is it counting one of my parents a child?是否将我的父母之一视为孩子? Does it matter if I have capital letters or lower case when creating a child?创建孩子时我是大写还是小写有关系吗? I realised that a child has to be lowercase for it to work as a child?我意识到一个孩子必须是小写的才能像孩子一样工作?

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


                        }

the problem is that you are trying to sort numeric string values.问题是您正在尝试对数字字符串值进行排序。 and java has a stupid problem with that so for example if you have these values in your list:并且 java 有一个愚蠢的问题,例如,如果您的列表中有这些值:

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

after sorting the order will be something like this:排序后的顺序将是这样的:

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

So one clean way would be to store your data in a class like so maybe:因此,一种干净的方法是将您的数据存储在一个类中,例如:

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

And then make an arrayList of People like so:然后像这样制作一个人的arrayList:

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

Then add your data to this arrayList and then sort it like this:然后将您的数据添加到此 arrayList 中,然后按如下方式对其进行排序:

peopleInfo.sort(Comparator.comparing(info -> info.name));

Also note that this type of sorting is case-sensitive.另请注意,这种类型的排序区分大小写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM