Collections에서sort 메서드 정렬

3324 단어
import java.util.*;  
//      Student  ,    C       !^_^  
class Student {  
    public int s_no;  
    public String s_name;  
    public int s_class;  
}  
  
public class compareTest {  
    public static void main(String[] args) {  
        //                
        ArrayList studentArr = new ArrayList();  
        Student s1 = new Student();  
        s1.s_no = 3;  
        s1.s_name = "a";  
        s1.s_class = 102;  
        studentArr.add(s1);  
        Student s2 = new Student();  
        s2.s_no = 2;  
        s2.s_name = "b";  
        s2.s_class = 101;  
        studentArr.add(s2);  
        Student s3 = new Student();  
        s3.s_no = 1;  
        s3.s_name = "c";  
        s3.s_class = 103;  
        studentArr.add(s3);  
        //                  
        System.out.println("   :");  
        for (int i = 0; i < studentArr.size(); i++) {  
            System.out  
                    .println("  " + studentArr.get(i).s_class + "  "  
                            + studentArr.get(i).s_name + "   "  
                            + studentArr.get(i).s_no);  
        }  
        //  Comparator       
        //           ,              。  
        Collections.sort(studentArr, new Comparator() {  
            public int compare(Object o1, Object o2) {  
                //              
                //        Object            ,     Student   
                //   s1,s2    s1,s2       ,               
                Student s1 = (Student) o1;  
                Student s2 = (Student) o2;  
                //              ,        ,  1               
                if (s1.s_no > s2.s_no) {  
                    return 1;  
                }  
                //      
                if (s1.s_no < s2.s_no) {  
                    return -1;  
                }  
                //    0            
                return 0;  
            }  
        });  
        //                   
        System.out.println("      :");  
        for (int i = 0; i < studentArr.size(); i++) {  
            System.out  
                    .println("  " + studentArr.get(i).s_class + "  "  
                            + studentArr.get(i).s_name + "   "  
                            + studentArr.get(i).s_no);  
        }  
        //             
        Collections.sort(studentArr, new Comparator() {  
            public int compare(Object o1, Object o2) {  
                Student s1 = (Student) o1;  
                Student s2 = (Student) o2;  
                if (s1.s_class > s2.s_class) {  
                    return 1;  
                }  
                if (s1.s_class < s2.s_class) {  
                    return -1;  
                }  
                return 0;  
            }  
        });  
        System.out.println("      :");  
        for (int i = 0; i < studentArr.size(); i++) {  
            System.out  
                    .println("  " + studentArr.get(i).s_class + "  "  
                            + studentArr.get(i).s_name + "   "  
                            + studentArr.get(i).s_no);  
        }  
    }  
}

좋은 웹페이지 즐겨찾기