数组和类集排序总结

一、基本数据类型(int、String等)

 1 package compare;
 2 
 3 import java.util.Arrays;
 4 
 5 public class ArrayDemo01 {
 6 
 7     public static void main(String[] args) {
 8         int temp[] = {3,5,7,9,1,2,6,8};
 9         Arrays.sort(temp);
10         
11         for(int i=0;i<temp.length;i++){  //升序排列
12             System.out.println(temp[i]);
13         }
14         for(int i=temp.length-1;i>=0;i--){  //降序排列
15             System.out.println(temp[i]);
16         }
17         //System.out.println(Arrays.toString(temp));
18         
19         //int Point = Arrays.binarySearch(temp, 3);
20         //System.out.println(Point);
21     }
22 
23 }

二、自定义数据类型构成的数组

  需要重写CompareTo()方法;接口Comparable接口中,只有这一个方法。

 1 package compare;
 2 
 3 class Student implements Comparable<Student>{
 4     private String name;
 5     public Student(String name){
 6         this.name = name;
 7     }
 8     public String toString(){
 9         return name;
10     }
11     public int compareTo(Student stu) {
12         if((this.name+stu.name).compareTo(stu.name+this.name)>0){
13             return -1;    
14         }else if((this.name+stu.name).compareTo(stu.name+this.name)<0){
15             return 1;
16         }else{
17             return 0;
18         }
19     }
20 }
21 
22 public class ComparableInterface {
23     public static void main(String[] args){
24         Student stu[] = {new Student("936"),
25                 new Student("93")};
26         java.util.Arrays.sort(stu);
27         for(int i=0;i<stu.length;i++){
28             System.out.print(stu[i]);
29         }
30     }
31 }

三、类集排序

ArrayList需要通过collections类的sort方法来进行排序,

如果想自定义排序方式则需要有类来实现Comparator接口并重写compare方法,

调用sort方法时将ArrayList对象与实现Commparator接口的类的对象作为参数。

 1 package arraylist_collections_comparator;
 2 
 3 import java.util.Comparator;
 4 
 5 class MyIntComparator implements Comparator{        
 6 /** 
 7 * o1比o2大,返回-1;o1比o2小,返回1。 
 8 */  
 9     public int compare(Object o1, Object o2) {  
10             int i1 = ((Integer)o1).intValue();  
11             int i2 = ((Integer)o2).intValue();  
12             if (i1 < i2){  
13                 return 1;  
14             }  
15             if (i1 > i2){  
16                 return -1;  
17             }  
18             return 0;  
19     }  
20 }  
 1 package arraylist_collections_comparator;
 2 
 3 import java.util.ArrayList;  
 4 import java.util.Collections;  
 5 import java.util.List;  
 6   
 7 /** 
 8 * 对List中的元素排序 
 9 */  
10 public class SortList {  
11     public static void output(List list){  
12         if (list == null){  
13             return;  
14         }  
15         for (int i=0; i<list.size(); i++){  
16             System.out.print(list.get(i).toString() + " ");  
17         }  
18         System.out.println();  
19     }  
20     public static void main(String[] args) {  
21         List list = new ArrayList();  
22         list.add(new Integer(5));  
23         list.add(new Integer(8));  
24         list.add(new Integer(1));  
25         list.add(new Integer(3));  
26         list.add(new Integer(2));  
27         //list.add(new Double(3.1)); 
28         Collections.sort(list);  
29         System.out.println("list被默认比较器排序后的状态");  
30         SortList.output(list); 
31         
32         //用自定义方法按降序排列</span>  
33         Collections.sort(list, new MyIntComparator());  
34         System.out.println("list被自定义比较器排序后的状态");  
35         SortList.output(list);  
36     }
37 }
原文地址:https://www.cnblogs.com/XuGuobao/p/7358374.html