1 public class DataSort { 2 3 public static void sort( int[] arr) { 4 for (int i = arr.length; i > 0; i--) { 5 for (int j = 0; j < i - 1; j++) { 6 // 若是前一個比後一個大,那麼就把大值交換到後面去 7 if (arr[j] > arr[j + 1]) { 8 int temp = arr[j]; 9 arr[j] = arr[j + 1]; 10 arr[j + 1] = temp; 11 } 12 } 13 } 14 } 15 }
測試類: html
1 public class Test { 2 3 public static void main(String[] args) { 4 int[] arr = new int[] { 9, 5, 2, 7 }; 5 DataSort. sort(arr); 6 for (int i : arr) { 7 System. out.print(i + " " ); 8 } 9 } 10 }
運行一下看看結果:算法
2 5 7 9
1 public class Person { 2 3 private String name ; 4 private int age; 5 private int money; 6 7 public Person(String name, int age, int money) { 8 this.name = name; 9 this.age = age; 10 this.money = money; 11 } 12 13 public String getName() { 14 return name ; 15 } 16 17 public void setName(String name) { 18 this.name = name; 19 } 20 21 public int getAge() { 22 return age ; 23 } 24 25 public void setAge(int age) { 26 this.age = age; 27 } 28 29 public int getMoney() { 30 return money ; 31 } 32 33 public void setMoney(int money) { 34 this.money = money; 35 } 36 37 @Override 38 public String toString() { 39 return "Person [name=" + name + ", age=" + age + ", money=" + money 40 + "]"; 41 } 42 43 } 44
Penson這個類定義完成了,怎麼進行排序呢,好比你說誰收入高誰老大,ok那麼咱們就按收入寫一下排序方法:設計模式
1 public class DataSort { 2 3 public static void sort( int[] arr) { 4 for (int i = arr.length; i > 0; i--) { 5 for (int j = 0; j < i - 1; j++) { 6 // 若是前一個比後一個大,那麼就把大值交換到後面去 7 if (arr[j] > arr[j + 1]) { 8 int temp = arr[j]; 9 arr[j] = arr[j + 1]; 10 arr[j + 1] = temp; 11 } 12 } 13 } 14 } 15 16 public static void sort(Person[] arr) { 17 for (int i = arr.length; i > 0; i--) { 18 for (int j = 0; j < i - 1; j++) { 19 // 若是前一個比後一個大,那麼就把大值交換到後面去 20 if (arr[j].getMoney() > arr[j + 1].getMoney()) { 21 Person temp = arr[j]; 22 arr[j] = arr[j + 1]; 23 arr[j + 1] = temp; 24 } 25 } 26 } 27 } 28 }
咱們在DataSort中重寫了一個sort(Person[] arr)方法,用來給Person類進行排序,測試一下吧:數組
1 public class Test { 2 3 public static void main(String[] args) { 4 // int[] arr = new int[] { 9, 5, 2, 7 }; 5 // DataSort.sort(arr); 6 // for (int i : arr) { 7 // System.out.print(i + " "); 8 // } 9 10 Person p1 = new Person("張三" , 25, 100); // 張三,25歲,年薪100w 11 Person p2 = new Person("李四" , 30, 10); // 李四,30歲,年薪10w 12 Person p3 = new Person("王五" , 20, 1000); // 王五,25歲,年薪1000w 13 Person[] arr = new Person[] { p1, p2, p3 }; 14 15 DataSort. sort(arr); 16 for (Person p : arr) { 17 System. out.println(p + " " ); 18 } 19 } 20 }
看下結果:框架
Person [name=李四, age=30, money=10] Person [name=張三, age=25, money=100] Person [name=王五, age=20, money=1000]
1 public interface MyComparable { 2 3 /** 4 * 返回值大於0說明當前比較的Object大,小於0說明被比較的Object大, 5 * 等於0說明兩個Object相等 6 */ 7 public int compareTo(Object o); 8 }
1 public class DataSort { 2 3 public static void sort(MyComparable[] arr) { 4 for (int i = arr.length; i > 0; i--) { 5 for (int j = 0; j < i - 1; j++) { 6 if (arr[j].compareTo(arr[j + 1]) > 0) { 7 MyComparable temp = arr[j]; 8 arr[j] = arr[j + 1]; 9 arr[j + 1] = temp; 10 } 11 } 12 } 13 } 14 15 }
是否是很簡單了,只要用compareTo的返回結果就能夠了,下面咱們讓Person實現MyComparable接口試一下:ide
1 public class Person implements MyComparable { 2 3 private String name ; 4 private int age; 5 private int money; 6 7 public Person(String name, int age, int money) { 8 this.name = name; 9 this.age = age; 10 this.money = money; 11 } 12 13 public String getName() { 14 return name ; 15 } 16 17 public void setName(String name) { 18 this.name = name; 19 } 20 21 public int getAge() { 22 return age ; 23 } 24 25 public void setAge(int age) { 26 this.age = age; 27 } 28 29 public int getMoney() { 30 return money ; 31 } 32 33 public void setMoney(int money) { 34 this.money = money; 35 } 36 37 @Override 38 public String toString() { 39 return "Person [name=" + name + ", age=" + age + ", money=" + money 40 + "]"; 41 } 42 43 @Override 44 public int compareTo(Object o) { 45 Person p = (Person)o; 46 if (this.money > p. money) { 47 return 1; 48 } else { 49 return -1; 50 } 51 } 52 53 }
測試一下:測試
1 public class Test { 2 3 public static void main(String[] args) { 4 // int[] arr = new int[] { 9, 5, 2, 7 }; 5 // DataSort.sort(arr); 6 // for (int i : arr) { 7 // System.out.print(i + " "); 8 // } 9 10 Person p1 = new Person("張三" , 25, 100); // 張三,25歲,年薪100w 11 Person p2 = new Person("李四" , 30, 10); // 李四,30歲,年薪10w 12 Person p3 = new Person("王五" , 20, 1000); // 王五,25歲,年薪1000w 13 Person[] arr = new Person[] { p1, p2, p3 }; 14 15 DataSort. sort(arr); 16 for (Person p : arr) { 17 System. out.println(p + " " ); 18 } 19 } 20 }
看一下結果:this
Person [name=李四, age=30, money=10] Person [name=張三, age=25, money=100] Person [name=王五, age=20, money=1000]
1 public interface MyComparator { 2 public int compare(Object o1, Object o2); 3 }
注意,這個接口不是讓你的排序類來實現的,看看我sort怎麼寫:spa
1 public class DataSort { 2 3 public static void sort(MyComparable[] arr) { 4 for (int i = arr.length; i > 0; i--) { 5 for (int j = 0; j < i - 1; j++) { 6 if (arr[j].compareTo(arr[j + 1]) > 0) { 7 MyComparable temp = arr[j]; 8 arr[j] = arr[j + 1]; 9 arr[j + 1] = temp; 10 } 11 } 12 } 13 } 14 15 public static void sort(Object[] arr, MyComparator c) { 16 for (int i = arr.length; i > 0; i--) { 17 for (int j = 0; j < i - 1; j++) { 18 if (c.compare(arr[j], arr[j + 1]) > 0) { 19 Object temp = arr[j]; 20 arr[j] = arr[j + 1]; 21 arr[j + 1] = temp; 22 } 23 } 24 } 25 } 26 27 }
1 public class PersonAgeComparator implements MyComparator { 2 3 @Override 4 public int compare(Object o1, Object o2) { 5 Person p1 = (Person) o1; 6 Person p2 = (Person) o2; 7 8 if (p1.getAge() - p2.getAge() > 0) { 9 return 1; 10 } else { 11 return -1; 12 } 13 } 14 15 }
具體看看怎麼來用:設計
1 public class Test { 2 3 public static void main(String[] args) { 4 // int[] arr = new int[] { 9, 5, 2, 7 }; 5 // DataSort.sort(arr); 6 // for (int i : arr) { 7 // System.out.print(i + " "); 8 // } 9 10 Person p1 = new Person("張三" , 25, 100); // 張三,25歲,年薪100w 11 Person p2 = new Person("李四" , 30, 10); // 李四,30歲,年薪10w 12 Person p3 = new Person("王五" , 20, 1000); // 王五,25歲,年薪1000w 13 Person[] arr = new Person[] { p1, p2, p3 }; 14 15 DataSort. sort(arr, new PersonAgeComparator()); 16 for (Person p : arr) { 17 System. out.println(p + " " ); 18 } 19 } 20 }
我只須要把個人比較大小邏輯類傳入sort就能夠了,看下結果:
Person [name=王五, age=20, money=1000] Person [name=張三, age=25, money=100] Person [name=李四, age=30, money=10]
1 public class Person implements MyComparable { 2 3 private String name ; 4 private int age; 5 private int money; 6 7 private MyComparator comparator = new PersonAgeComparator(); 8 9 public Person(String name, int age, int money) { 10 this.name = name; 11 this.age = age; 12 this.money = money; 13 } 14 15 public Person(String name, int age, int money, MyComparator comparator) { 16 this.name = name; 17 this.age = age; 18 this.money = money; 19 this.comparator = comparator; 20 } 21 22 public String getName() { 23 return name ; 24 } 25 26 public void setName(String name) { 27 this.name = name; 28 } 29 30 public int getAge() { 31 return age ; 32 } 33 34 public void setAge(int age) { 35 this.age = age; 36 } 37 38 public int getMoney() { 39 return money ; 40 } 41 42 public void setMoney(int money) { 43 this.money = money; 44 } 45 46 @Override 47 public String toString() { 48 return "Person [name=" + name + ", age=" + age + ", money=" + money 49 + "]"; 50 } 51 52 @Override 53 public int compareTo(Object o) { 54 return comparator .compare(this, o); 55 } 56 57 }
1 public V put(K key, V value) { 2 ...... 3 ...... 4 5 // split comparator and comparable paths 6 // 當前使用的比較器 7 Comparator<? super K> cpr = comparator ; 8 // 若是比較器不爲空,就是用指定的比較器來維護TreeMap的元素順序 9 if (cpr != null) { 10 // do while循環,查找key要插入的位置(也就是新節點的父節點是誰) 11 do { 12 // 記錄上次循環的節點t 13 parent = t; 14 // 比較當前節點的key和新插入的key的大小 15 cmp = cpr.compare(key, t. key); 16 // 新插入的key小的話,則以當前節點的左孩子節點爲新的比較節點 17 if (cmp < 0) 18 t = t. left; 19 // 新插入的key大的話,則以當前節點的右孩子節點爲新的比較節點 20 else if (cmp > 0) 21 t = t. right; 22 else 23 // 若是當前節點的key和新插入的key想的的話,則覆蓋map的value,返回 24 return t.setValue(value); 25 // 只有當t爲null,也就是沒有要比較節點的時候,表明已經找到新節點要插入的位置 26 } while (t != null); 27 } 28 else { 29 // 若是比較器爲空,則使用key做爲比較器進行比較 30 // 這裏要求key不能爲空,而且必須實現Comparable接口 31 if (key == null) 32 throw new NullPointerException(); 33 Comparable<? super K> k = (Comparable<? super K>) key; 34 // 和上面同樣,喜歡查找新節點要插入的位置 35 do { 36 parent = t; 37 cmp = k.compareTo(t. key); 38 if (cmp < 0) 39 t = t. left; 40 else if (cmp > 0) 41 t = t. right; 42 else 43 return t.setValue(value); 44 } while (t != null); 45 } 46 47 ...... 48 ...... 49 }
1 public interface Comparator<T> { 2 int compare(T o1, T o2); 3 boolean equals(Object obj); 4 }
1 public interface Comparable<T> { 2 public int compareTo(T o); 3 }
惟一不一樣的是Comparator接口中要求重寫equals方法,用於比較是否相等。