In the programmer's interview, everyone should have seen a variety of sorting methods:
Bubble
Choose
Insert
Fast
Heap Sort
Wait a minute..
These are simple data types, such as the numeric type int, when encountered in the actual situation of the complex sort, such as to a company's employees, according to the name, age, gender and other factors of the comprehensive sorting situation, with the above sorting method, will not be realized. But don't be afraid. There are big artifacts in ~java: Comparator interface
Let's look at the source code, simply so you don't know what to do.
Public interface Comparator<t> {/** * compares its-arguments for order. Returns a negative integer, * zero, or a positive integer as the first argument is less than, equal * to, or great ER than the second.<p> * * In the foregoing description, the notation * <TT>SGN (</TT><I&G t;expression</i><tt>) </tt> designates the mathematical * <i>signum</i> function, which I s defined to return one of the <tt>-1</tt>, * <TT>0</TT>, or <tt>1</tt> according to W Hether the value of * <i>expression</i> is negative, zero or positive.<p> * * the Implemento R must ensure that <TT>SGN (compare (x, y)) = = *-SGN (compare (y, x)) </tt> for all <tt>x</tt> and <tt>y</tt>. (this * implies that <tt>compare (x, y) </tt> must throw a exception if and only * if <tt>compare (y, X) </tt> throws an EXception.) <p> * The implementor must also ensure, the relation is transitive: * <tt> ((compare (x, y) ; 0) && (compare (Y, z) >0) </tt> implies * <tt>compare (x, z) >0</tt>.<p> * * Finally, the implementor must ensure that <tt>compare (x, y) ==0</tt> * implies that <TT>SGN (compare (x, Z)) ==SGN (compare (Y, z)) </tt> for all * <tt>z</tt>.<p> * * It's generally the case, but & Lt;i>not</i> strictly required that * <tt> (compare (x, y) ==0) = = (X.equals (y)) </tt>. Generally speaking, * any comparator it violates this condition should clearly indicate * this fact. The recommended language is "note:this comparator * imposes orderings, that's inconsistent with equals." * * @param O1 The first object to be compared. * @param O2 the second object to be compared. * @return A negative integer, zero, or a positive intEger as the * First argument is less than, equal to, or greater than the * second. * @throws NullPointerException If an argument are null and this * comparator does not permit null arguments * @throws classcastexception If the arguments ' types prevent them from * being compared by this comparator. */<span style= "FONT-SIZE:18PX;" > int compare (t O1, T O2);</span>/** * Indicates whether some other object is ' equal to ' this * comp Arator. This method must obey the contract of * {@link object#equals (Object)}. Additionally, this method can return * <tt>true</tt> <i>only</i> If the specified object is AL So a comparator * and it imposes the same ordering as this comparator. Thus, * <code>comp1.equals (COMP2) </code> implies that <TT>SGN (Comp1.compare (O1, * O2)) ==SGN (comp 2.compare (O1, O2)) </tt> for every object reference * <TT>o1</tt> and <tt>o2</tt>.<p> * * Note that it's <i>always</i> safe <i> Not</i> to override * <tt>object.equals (Object) </tt>. However, overriding this method could, * in some cases, improve performance by allowing programs to determine * that Distinct comparators impose the same order. * * @param obj the Reference object with which to compare. * @return <code>true</code> only if the specified object is also * a comparator and it imposes t He same ordering as this * comparator. * @see object#equals (Object) * @see object#hashcode () */<span style= "FONT-SIZE:18PX;" > Boolean equals (Object obj); </span>}As for the 2 methods, we can implement the complex sort, so we just have to implement the compare interface in the interface to be OK.
The specific code is as follows:
Package Sometest;import java.util.comparator;class Person {String firstname,lastname; Boolean sex; Integer age; Public person (String firstname,string Lastname,boolean Sex,integer age) {this.firstname = FirstName; This.lastname = LastName; This.sex = sex; This.age = age; } public String Getfirstname () {return firstname; } public String Getlastname () {return LastName; } public Boolean Getsex () {return sex; } public Integer Getage () {return age; }//For ease of input, rewrite ToString () public String ToString () {return FirstName + "" +lastname+ "" + (Sex.booleanvalue () ?" Male ":" female ") +" "+age; }}//end person public class Compareobject implements comparator<object> {@Overridepublic int compare (Object O1 , Object O2) {if (O1 instanceof String) {return Compareimp ((string) O1, (string) O2); }else if (O1 instanceof Integer) {RETUrn compareimp ((integer) O1, (integer) O2); }else if (O1 instanceof Boolean) {return Compareimp ((Boolean) O1, (Boolean) O2); } else if (O1 instanceof person) {return compareimp (person) O1, (person) O2); }else {//Expand compare function System.err.println as required ("no suitable comparator found"); return 1; }}//Reload public int Compareimp (string O1, String O2) {string S1 = (string) O1; String s2 = (string) O2; int len1 = S1.length (); int len2 = S2.length (); int n = math.min (len1, len2); Char v1[] = S1.tochararray (); Char v2[] = S2.tochararray (); int pos = 0; while (n--! = 0) {char C1 = V1[pos]; char C2 = V2[pos]; if (c1! = C2) {return c1-c2; } pos++; } return len1-len2; }//Reload public int compareimp (Integer O1, Integer O2) {int val1 = O1.intvalue (); int val2 = O2.intvalue (); Return (Val1 < Val2 1: (Val1 = = val2? 0:1)); }//Overload public int Compareimp (Boolean O1, Boolean O2) {return (O1.equals (O2)? 0: (O1.booleanvalue () ==true?1:-1)); }/* The sort of entity class * is sorted by last name, who has the first name of the alphabet, and who is in front of the line. * Then sort the names. If you have the same name, women are ahead. * If the name and gender are the same, the younger platoon head. * *///Reload public int compareimp (person O1, person O2) {String firstname1 = O1.getfirstname (); String firstname2 = O2.getfirstname (); String lastname1 = O1.getlastname (); String lastname2 = O2.getlastname (); Boolean sex1 = O1.getsex (); Boolean sex2 = O2.getsex (); Integer age1 = O1.getage (); Integer age2 = O2.getage (); int comparefirstname = Compare (firstname1, firstname2); int comparelastname = Compare (lastname1, lastname2); int comparesex = Compare (Sex1, sex2); if (comparefirstname! = 0) {return comparefirstname; } if (Comparelastname! = 0) {return comparelastname; } if (Comparesex! = 0) {return comparesex; } return Compare (Age1, age2); }/*public Boolean equals (Object obj) {return true;} */public static void Main (string[] args) {person[] person = new person[] {new Person ("Zhangsan", "GG", Boolea N.true, new integer), new person ("Lisi", "MM", Boolean.true, new Integer), and new person ("Wangwu" , "MM", Boolean.false, New integer), new person ("Wangwu", "MM", Boolean.false, new Integer (10))}; for (int i = 0; i < person.length; i++) {if (i==0) System.out.println ("before sorting:"); System.out.println ("before sort=" + person[i]); } java.util.Arrays.sort (Person, New Compareobject ()); for (int i = 0; i < person.length; i++) {if (i==0) System.out.println ("After sorting:"); System.out.println ("after sort= "+ person[i]); } }}Operation Result:
Before sorting:
Before Sort=zhangsan GG Male 27
Before sort=lisi mm Male 27
Before Sort=wangwu mm Female 27
Before Sort=wangwu mm female 10
After sorting:
After sort=lisi mm Male 27
After Sort=wangwu mm female 10
After Sort=wangwu mm Female 27
After Sort=zhangsan GG Male 27
Note: when implementing the comparator interface, the Equals method is not implemented, and the program does not have an error because the class that implements the interface is also a subclass of the object class, and the object class has implemented the Equals method