When the collection or array that needs to be sorted is not a simple numeric type, you can usually use Comparator or Comparable to implement object sorting or custom sorting in a simple way.
A comparison function, which imposes a total ordering on some collection of objects. Comparators can be passed to a sort method (such as Collections.sort or Arrays.sort) to allow precise control over the sort order. Comparators can also be used to control the order of certain data structures (such as sorted sets or sorted maps), or to provide an ordering for collections of objects that don't have a natural ordering. ------ API
You can sort the string List directly because the String object has helped us implement the Comparable interface, so if our Person wants to sort, we also need to implement a comparator.
1. Comparator
Sort objects stored in Linkedlist
import java.util.Comparator;import java.util.LinkedList;class Person{ private float height; private String name; Person(float height) { this.height=height; } public float getHeight() { return height; } public void setHeight(float height) { this.height = height; } public String getName() { return name; } public void setName(String name) { this.name = name; }}class PersonHeight implements Comparator<Person>{ @Override //Rewrite the compare method, return<0 remains unchanged, return>0 is exchanged in sequence (keep ascending order) public int compare(Person e1, Person e2) { if(e1.getHeight() < e2.getHeight()){ return 1; } else { return -1; } }}public class Question3 { public static void main(String[] args) { Person p1=new Person(23.4f); p1.setName("Stud1"); Person p2=new Person(2.34f); p2.setName("Stud2"); Person p3=new Person(34.32f); p3.setName("Stud3"); Person p4=new Person(56.45f); p4.setName("Stud4"); Person p5=new Person(21.4f); p5.setName("Stud5"); LinkedList<Person> al=new LinkedList<Person>(); al.add(p1); al.add(p2); al.add(p3); al.add(p4); al.add(p5); //Call the sort method to implement sorting Collections.sort(al, new PersonHeight()); //Transip the output for(Person p:al) System.out.println(p.getName()); }}
Additional:
//Sort the date/** * If o1 is less than o2, return a negative number; if o1 is greater than o2, return a positive number; if they are equal, return 0; */@Overridepublic int compare(Step o1, Step o2) { Date acceptTime1=UtilTool.strToDate(o1.getAcceptTime(), null); Date acceptTime2=UtilTool.strToDate(o2.getAcceptTime(), null); //Ascending the date field, if you want to descend, you can use the before method if(acceptTime1.after(acceptTime2)) return 1; return -1;}2. Comparable
import java.util.Collections;import java.util.Comparator;import java.util.LinkedList;class Person implements Comparable{ private float height; private String name; Person(float height) { this.height=height; } public float getHeight() { return height; } public void setHeight(float height) { this.height = height; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public int compareTo(Object o) { // TODO Auto-generated method stub if(this.height>((Person)o).height){ return 1; }else return -1; } }public class Question3 { public static void main(String[] args) { Person p1=new Person(23.4f); p1.setName("Stud1"); Person p2=new Person(2.34f); p2.setName("Stud2"); Person p3=new Person(34.32f); p3.setName("Stud3"); Person p4=new Person(56.45f); p4.setName("Stud4"); Person p5=new Person(21.4f); p5.setName("Stud5"); LinkedList<Person> al=new LinkedList<Person>(); al.add(p1); al.add(p2); al.add(p3); al.add(p4); al.add(p5); Collections.sort(al); for(Person p:al) System.out.println(p.getName()); }}Three. Comparison
Comparable is defined inside the Person class .
Comparator is defined outside of Person. At this time, the structure of our Person class does not need to change.
The two methods have their own advantages and disadvantages. It is simple to use Comparable. As long as the object that implements the Comparable interface becomes a comparable object, it requires modifying the source code. The advantage of using Comparator is that it does not require modifying the source code, but implementing a comparator. When a custom object needs to be compared, you can compare the size by passing the comparator and the object together. In Comparator, users can implement complex and general logic by themselves, so that they can match some relatively simple objects, which can save a lot of repetitive labor.