Objects exist in an array or set, and data in the set needs to be sorted frequently. in Java, the collections and arrays classes encapsulate the sort method to sort an array or set.
Based on the preceding conditions, you can configure the comparator method to sort object information according to the conditions you need.
The nine basic types of comparator based on Java are completed here. Because the basic types have implemented the comparable interface, it is convenient to implement it. The exception handling is not perfect. If you are interested, please complete this part of code.
// Convenience and efficiency problems // because reflection is used to obtain the basic type value in the object, the returned value is the packaging type // The packaging type implements the comparable interface, therefore, you can obtain its compareto method public abstract class genericcomparator <t> implements comparator <t> {public static final Boolean ASC = true; public static final Boolean DESC = false; private Static final class <?> [] Base_classes = {integer. class, String. class, double. class, float. class, Boolean. class, character. class, long. class, short. class, byte. class}; private class <t> clz; private Boolean order = ASC; private string property; private string compareproperty; private method comparemethod; Public genericcomparator (string Property) throws exception {// load the generic type @ suppresswarnings ("unchecked") class <t> temp = (class <t>) (Parameterizedtype) getclass (). getgenericsuperclass ()). getactualtypearguments () [0]; clz = temp; // set the Property (property);} public genericcomparator <t> Order (Boolean order) {This. order = order; return this;} public genericcomparator <t> property (string property) throws exception {This. property = property; try {Init (); // initialize} catch (exception e) {e. printstacktrace (); throw E;} return this ;}@ Overridepublic int compare (T T1, t T2) {object ov1 = NULL; object ov2 = NULL; int result = 0; try {ov1 = value (T1 ); ov2 = value (T2); Result = (integer) comparemethod. invoke (ov1, ov2);} catch (exception e) {e. printstacktrace (); system. exit (0); // if a problem occurs, exit.} return order? Result:-result;} // initialize the comparison parameter private void Init () throws securityexception, nosuchfieldexception, nosuchmethodexception, illegalargumentexception, illegalaccessexception, instantiationexception {If (compareproperty! = Property) {compareproperty = property; comparemethod = method () ;}// obtain the compareto method private method () throws securityexception, nosuchfieldexception, nosuchmethodexception, expiration, expiration, instantiationexception {object o = value (clz. newinstance (); Class <?> V = NULL; If (o! = NULL) {// base type has the valuev = O. getclass ();} else {// package type, checked the class in the base_classesfield F = clz. getdeclaredfield (property); V = f. getType () ;}if (! Contains (V) {Throw new nosuchmethodexception ("the class you used is not based type. ");} return v. getmethod ("compareto", v) ;}// obtain the private object value (t o) to be compared for the object throws securityexception, nosuchfieldexception, illegalargumentexception, illegalaccessexception {field F = O. getclass (). getdeclaredfield (property); If (! F. isaccessible () {f. setaccessible (true);} return F. get (o) ;}// verify whether it is a valid data type private Boolean contains (class <?> C) {for (class <?> Cc: base_classes) {If (CC. Equals (c) {return true ;}} return false ;}}
Careful friends have discovered that the above Code is an abstract class, the reason is:
In the constructor, getgenericsuperclass is used to obtain its parent class information to obtain its generic information.