When you want to sort a collection or array that is not a simple numeric type, you can usually use comparator or comparable to implement an object sort or custom sort 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 Sor T order. Comparators can also is used to control the order of certain data structures (such as sorted sets or sorted), or to P Rovide an ordering to collections of objects that don ' t have a natural ordering. ------API
The list of strings can be sorted directly, because the string object has helped us implement the comparable interface, so our person will implement a comparer if they want to sort.
I. Comparator
Sort the objects stored by the 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//Override compare method, return<0 unchanged, return>0 Exchange order
(keep ascending) 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 the sort Collections.sort (Al, New Personheight ());
Traverse output for (person P:al) System.out.println (P.getname ()); }
}
Additional:
Sort dates
/**
* Returns a negative number if the O1 is less than O2, returns a positive number if the O1 is greater than O2, and returns 0
if they are equal; */
@Override public
int compare (step O1, step O2) {
Date accepttime1=utiltool.strtodate o1.getaccepttime ( ), null);
Date accepttime2=utiltool.strtodate (O2.getaccepttime (), null);
The Date field is ascending, and if you want to descending, use the Before method if
(Accepttime1.after (acceptTime2)) return 1;
return-1;
}
Two. 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>).
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. Compare
Comparable is defined within the person class .
Comparator is defined outside of person, at which point the structure of our person class does not require any change.
Two methods have advantages and disadvantages, with comparable simple, as long as the object to achieve comparable interface directly become a can be compared to the object, but need to modify the source code, with the advantage of comparator is not necessary to modify the source code, but also to implement a comparator, When a custom object needs to be compared, passing the comparator to the object in the past can be larger than the size, and within the comparator the user can implement the complex and universal logic that can be used to match some of the simpler objects, which can save a lot of repetitive work.