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 }
测试类:
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接口试一下:
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 }
看一下结果:
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怎么写:
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方法,用于比较是否相等。