排序

2023-11-13  本文已影响0人  皓皓amous
   private void initSort() {
       Collections.sort(listStudents, new Comparator<Student>() {
           @Override
           public int compare(Student student, Student t1) {
               int age = student.getAge();
               int age1 = t1.getAge();

               int weight = student.getWeight();
               int weight1 = t1.getWeight();

//                return age - age1;
               if (age > age1) {
                   return 1;
               } else if (age < age1) {
                   return -1;
               } else {
                   if (weight > weight1) {
                       return -1;
                   } else if (weight < weight1) {
                       return 1;
                   } else {
                       return 0;
                   }

               }
           }
       });

       String test = "";
       for (Student list : listStudents) {
           test = test + "年:" + list.getAge() + "体:" + list.getWeight() + ",  ";
       }
       Log.d(TAG, "testSQ" + " " + test);

   }

   private void initData() {
       if (listStudents == null) {
           listStudents = new ArrayList<>();
       }
       listStudents.clear();
       Student one = new Student(17, "占山", 117);
       Student two = new Student(15, "二胡", 125);
       Student three = new Student(19, "王威", 112);
       Student four = new Student(12, "陈浩", 98);
       Student five = new Student(19, "陈伟", 130);
       Student six = new Student(21, "你都", 78);
       Student sive = new Student(20, "以后", 234);
       Student eight = new Student(19, "余生", 136);
       Student night = new Student(16, "kids的", 123);

       listStudents.add(one);
       listStudents.add(two);
       listStudents.add(three);
       listStudents.add(four);
       listStudents.add(five);
       listStudents.add(six);
       listStudents.add(sive);
       listStudents.add(eight);
       listStudents.add(night);

   }
上一篇 下一篇

猜你喜欢

热点阅读