Java对象排序


  1. import java.util.Arrays;  
  2. import java.util.Comparator;  
  3.   
  4. public class TestSort {  
  5.   
  6.     public static void main(String[] args) {  
  7.         TestSort ts = new TestSort();  
  8.   
  9.         Integer[] it = new Integer[10];  
  10.         for (int i = 0; i < 10; i++) {  
  11.             it[i] = new Integer(10 - i);  
  12.         }  
  13.         ts.mergeSort(it, 0, it.length - 1null);  
  14.         System.out.println(Arrays.toString(it));  
  15.         // 输出结果为:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]   
  16.   
  17.         // 上面是对Integer对象进行排序   
  18.         /*************************************************************/  
  19.         // 下面是对Person对象进行排序   
  20.   
  21.         PersonCompator pc = new PersonCompator();  
  22.         Person[] per = new Person[] { new Person(23"a"), new Person(12"b"),  
  23.                 new Person(1"c"), new Person(44"d") };  
  24.         ts.mergeSort(per, 0, per.length - 1, pc);  
  25.         System.out.println(Arrays.toString(per));  
  26.         // 输出结果为:[c:1, b:12, a:23, d:44]   
  27.     }  
  28.   
  29.     /* 
  30.      * 对于对象比较有两种方法,第一种是重写此类,实现Compareable接口,Integer类就是这么做的, 
  31.      * 所以此时可以直接将所有对象自动向上转型为Object,再对其进行比较,进而排序 
  32.      *  
  33.      * 第二种则是写一个此类的比较类,实现Comparator,下面的Person类就是这么干的,此时只需调用 比较类的compare方法即可 
  34.      */  
  35.     private <T> void mergeSort(T[] arr, int i, int j, Comparator<? super T> c) {  
  36.         if (i < j) {  
  37.             int k = (i + j) / 2;  
  38.             mergeSort(arr, i, k, c);  
  39.             mergeSort(arr, k + 1, j, c);  
  40.             merge(arr, i, k, j, c);  
  41.         }  
  42.     }  
  43.   
  44.     private <T> void merge(T[] arr, int p, int r, int q, Comparator<? super T> c) {  
  45.         int n1 = r - p + 1;  
  46.         int n2 = q - r;  
  47.         T[] L = arr.clone();  
  48.         System.arraycopy(arr, p, L, 0, n1);  
  49.         T[] R = arr.clone();  
  50.         System.arraycopy(arr, r + 1, R, 0, n2);  
  51.         int i = 0, j = 0, k = p;  
  52.         if (c == null) {  
  53.             while (i < n1 && j < n2) {  
  54.                 if (((Comparable<T>) L[i]).compareTo(R[j]) <= 0) {  
  55.                     arr[k++] = L[i++];  
  56.                 } else {  
  57.                     arr[k++] = R[j++];  
  58.                 }  
  59.             }  
  60.         } else {  
  61.             while (i < n1 && j < n2) {  
  62.                 if (c.compare(L[i], R[j]) <= 0) {  
  63.                     arr[k++] = L[i++];  
  64.                 } else {  
  65.                     arr[k++] = R[j++];  
  66.                 }  
  67.             }  
  68.         }  
  69.         while (i < n1) {  
  70.             arr[k++] = L[i++];  
  71.         }  
  72.         while (j < n2) {  
  73.             arr[k++] = R[j++];  
  74.         }  
  75.     }  
  76. }  
  77.   
  78. class Person {  
  79.   
  80.     private int age;  
  81.     private String name;  
  82.   
  83.     public Person() {  
  84.   
  85.     }  
  86.   
  87.     public Person(int a, String n) {  
  88.         this.setAge(a);  
  89.         this.setName(n);  
  90.     }  
  91.   
  92.     public int getAge() {  
  93.         return age;  
  94.     }  
  95.   
  96.     public void setAge(int age) {  
  97.         this.age = age;  
  98.     }  
  99.   
  100.     public String getName() {  
  101.         return name;  
  102.     }  
  103.   
  104.     public void setName(String name) {  
  105.         this.name = name;  
  106.     }  
  107.   
  108.     public String toString() {  
  109.         return this.getName() + ":" + this.getAge();  
  110.     }  
  111. }  
  112.   
  113. class PersonCompator implements Comparator<Person> {  
  114.     public int compare(Person o1, Person o2) {  
  115.         return o1.getAge() - o2.getAge();  
  116.     }  
  117. }  

相关内容