We in the actual development work, often encounter the problem of sorting, such as the title, how do we sort for a particular set of properties List
When the element type list in the collection is our custom type, there are two ways to sort the list of elements of:
method one
Let the element type list in the collection, which is our custom class that implements Comparable <T> interface, and write public int compareTo (T o) method in the class. As follows:
1 public class Test 2 { 3 public static void main(String[] args) 4 { 5 ArrayList<Entity> list=new ArrayList<Entity>(); 6 list.add(new Entity("李四",24)); 7 list.add(new Entity("张三",13)); 8 list.add(new Entity("王五",25)); 9 System.out.println("排序前:"+list); 10 Collections.sort(list); 11 System.out.println("排序后:"+list); 12 } 13 } 14 class Entity implements Comparable<Entity> 15 { 16 String name; 17 int age; 18 public Entity(String name, int age) { 19 super(); 20 this.name = name; 21 this.age = age; 22 } 23 public String getName() { 24 return name; 25 } 26 public void setName(String name) { 27 this.name = name; 28 } 29 public int getAge() { 30 return age; 31 } 32 public void setAge(int age) { 33 this.age = age; 34 } 35 @Override 36 public String toString() { 37 return "Entity [name=" + name + ", age=" + age + "]"; 38 } 39 @Override 40 public int compareTo(Entity o) 41 { 42 if(this.getAge()>o.getAge()) 43 { 44 return 1; 45 } 46 else if(this.getAge()<o.getAge()) 47 { 48 return -1; 49 } 50 else 51 { 52 return 0; 53 } 54 } 55 }
We want this type of Entity according to age property values in ascending order, the first method is to let the Entity class that implements Comparable <Entity> interface, and write public int compareTo (Entity o) method. Thus using the Collections.sort (list); age can be sorted on the size of the object list.
Output:
Before ordering: [Entity [name = John Doe, age = 24], Entity [ name = Joe Smith, age = 13], Entity [ name = Wang Wu, Age = 25 ]]
Sorted: [the Entity [name = Joe Smith, age = 13], Entity [ name = John Doe, age = 24], Entity [ name = Wang Wu, age = 25]]
Method Two
Method two is in the sort of time to sort () method passing in a comparator. Specifically, it is passed to achieve a relatively anonymous inner class interface, the purpose is to tell the sort () method to sort the list of objects in accordance with the comparator. As follows:
1 public class Test 2 { 3 public static void main(String[] args) 4 { 5 ArrayList<Entity> list=new ArrayList<Entity>(); 6 list.add(new Entity("李四",24)); 7 list.add(new Entity("张三",13)); 8 list.add(new Entity("王五",25)); 9 System.out.println("排序前:"+list); 10 Collections.sort(list,new Comparator<Entity>() { 11 @Override 12 public int compare(Entity o1, Entity o2) 13 { 14 if(o1.getAge()>o2.getAge()) 15 { 16 return 1; 17 } 18 else if(o1.getAge()<o2.getAge()) 19 { 20 return -1; 21 } 22 else 23 { 24 return 0; 25 } 26 } 27 }); 28 System.out.println("排序后:"+list); 29 } 30 } 31 class Entity 32 { 33 String name; 34 int age; 35 public Entity(String name, int age) { 36 super(); 37 this.name = name; 38 this.age = age; 39 } 40 public String getName() { 41 return name; 42 } 43 public void setName(String name) { 44 this.name = name; 45 } 46 public int getAge() { 47 return age; 48 } 49 public void setAge(int age) { 50 this.age = age; 51 } 52 @Override 53 public String toString() { 54 return "Entity [name=" + name + ", age=" + age + "]"; 55 } 56 }
When a sort () method when the incoming comparator, sort () method will be used to sort the list of objects from the comparison device.
Output:
Before ordering: [Entity [name = John Doe, age = 24], Entity [ name = Joe Smith, age = 13], Entity [ name = Wang Wu, Age = 25 ]]
Sorted: [the Entity [name = Joe Smith, age = 13], Entity [ name = John Doe, age = 24], Entity [ name = Wang Wu, age = 25]]
The sort () method Why can sort the String data type it?
The answer is, String class implements Comparable <String> interface. It is possible to directly Collections.sort (list); String type of data to be sorted.