JAVA uses comparator to implement custom sorting

Source: Internet
Author: User

/*

There is a person class, including last name, first name, gender, age attribute: Now there is a need to sort the following rules for person
* whose surname Pinyin is on the front, who is in line.
* Then sort the names. If you have the same name, women are ahead.
* If the name and sex are the same, the younger is in the front row.
*
int compare (object O1, Object O2) returns an integer of the base type
If you want to sort in ascending order,
O1 is less than O2, returns-1 (negative numbers), equality returns 0,01 greater than 02 returns 1 (positive numbers)
If you want to sort in descending order
Then O1 is less than O2, returns 1 (positive numbers), equality returns 0,01 greater than 02 returns-1 (negative)


*/

Person class

Package com.ibm.test;

public class Person {
String Firstname,lastname;
char sex;
Integer age;

Public person (string firstname, string lastname, char sex, Integer age) {

This.firstname = FirstName;
This.lastname = LastName;
This.sex = sex;
This.age = age;
}
Public Integer Getage () {
return age;
}
public void Setage (Integer age) {
This.age = age;
}
Public String Getfirstname () {
return FirstName;
}
public void Setfirstname (String firstname) {
This.firstname = FirstName;
}
Public String Getlastname () {
return lastname;
}
public void Setlastname (String lastname) {
This.lastname = LastName;
}
Public Char getsex () {
return sex;
}
public void Setsex (char sex) {
This.sex = sex;
}


}

Comparators class:

Package com.ibm.test;

/*
* whose surname Pinyin is on the front, who is in line.
* Then sort the names. If you have the same name, women are ahead.
* If the name and sex are the same, the younger is in the front row.
*
int compare (object O1, Object O2) returns an integer of the base type
If you want to sort in ascending order,
O1 is less than O2, returns-1 (negative numbers), equality returns 0,01 greater than 02 returns 1 (positive numbers)
If you want to sort in descending order
Then O1 is less than O2, returns 1 (positive numbers), equality returns 0,01 greater than 02 returns-1 (negative)


*/
public class Comparators {
public static Java.util.Comparator Getcomparator () {
return new Java.util.Comparator () {

public int Compare (object O1, Object O2) {
if (O1 instanceof String) {
Return compare ((String) O1, (string) O2);
}else if (O1 instanceof Integer) {
return compare ((integer) O1, (integer) O2);
}else if (O1 instanceof person) {
return compare (person) O1, (person) O2);
}else {
SYSTEM.ERR.PRINTLN ("No suitable comparator found");
return 1;
}
}

public int Compare (string O1, string O2) {

string S1 = (string) O1;
String s2 = (string) O2;
/*
* SYSTEM.OUT.PRINTLN ("s1==" +s1+ "s2==" +s2);
* Remove the array adjacent to the two surname FirstName or name LastName
*/
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;
}

public int Compare (integer o1, integer o2) {

int val1 = O1.intvalue ();
int val2 = O2.intvalue ();

/*
* SYSTEM.OUT.PRINTLN ("val1==" +val1+ "val2==" +val2);
* Take out the age of two persons adjacent to the array
*/
Return (Val1 < val2-1: (Val1 = = val2? 0:1));

}
public int Compare (char O1, char O2) {
/*
* SYSTEM.OUT.PRINTLN ("o1==" +o1+ "o2==" +o2);
* Remove the gender of the two persons adjacent to the array
* Logical explanation of return:
*
For example, the sex of two persons is male and female respectively, when the first person's sex is ' male ', then returns 1
(returns 1 for the Compare method, that is, O1 (male) > (female) O2, and O1>o2 return 1 is sorted in ascending order, ascending sort is the small (female) in the front)

Still do not understand the words: then look at the interpretation of the Compare method
int compare (object O1, Object O2) returns an integer of the base type
If you want to sort in ascending order,
O1 is less than O2, returns-1 (negative numbers), equality returns 0,01 greater than 02 returns 1 (positive numbers)
If you want to sort in descending order
Then O1 is less than O2, returns 1 (positive numbers), equality returns 0,01 greater than 02 returns-1 (negative)

*/

Return ((O1==O2) 0: (o1== ' man '/1:-1));

}

public int Compare (person O1, person O2) {

String firstname1 = O1.getfirstname ();
String firstname2 = O2.getfirstname ();
String lastname1 = O1.getlastname ();
String lastname2 = O2.getlastname ();
Char sex1 = O1.getsex ();
Char sex2 = O2.getsex ();
Integer age1 = O1.getage ();
Integer age2 = O2.getage ();
/*return logical explanation is as follows:
* whose surname is Pinyin FirstName Front, who is ahead.
* Then sort the name LastName. If you have the same name,
* Female sex==false ahead of the line. (Sex value True indicates male, false indicates female)
* If the name and sex are the same, the age of the younger is in the front row.
*/
Return (compare (firstname1, firstname2) = = 0?
(Compare (lastname1, lastname2) = = 0? (Compare (sex1, sex2) = = 0? (Compare (Age1, age2) = = 0? 0:
Compare (Age1, age2)):
Compare (Sex1, sex2)):
Compare (Lastname1, lastname2)):
Compare (Firstname1, firstname2));

}

};
}

}

Main method

Package com.ibm.test;

Import Java.util.Arrays;

/*
* whose surname Pinyin is on the front, who is in line.
* Then sort the names. If you have the same name, women are ahead.
* If the name and sex are the same, the younger is in the front row.
*


*/
public class Main {
Public Main () {
}
public static void Main (string[] args) {
person[] person = new person[] {
New Person ("Ouyang", "Feng", ' Male ', new Integer (27)),
New Person ("Zhuang", "GW", ' Male ', new Integer (27)),
New Person ("Deng", "jx", ' Male ', new Integer (28)),
New Person ("Deng", "JX", ' female ', new Integer (27)),


};

for (int i = 0; i < person.length; i++) {
System.out.println ("Sort before =" + person[i].getfirstname () + "" +person[i].getlastname () + "" +person[i].getage () + "" +person[ I].getsex ());
}
/*
* According to the first parameter of the sort method is person, and then find the corresponding method in the Comparators.getcomparator () method
* Compare (person O1, Person O2), with parameters consistent with person type
*/
Java.util.Arrays.sort (person, comparators.getcomparator ());

System.out.println ("————————————————————————————————————————————————————————————————————————");
for (int i = 0; i < person.length; i++) {
System.out.println ("sort =" + person[i].getfirstname () + "" +person[i].getlastname () + "" +person[i].getage () + "" +person[ I].getsex ());
}
System.out.println ("————————————————————————————————————————————————————————————————————————");

integer[] data = new integer[]{2,3,1};

for (int i = 0; i < data.length; i++) {
System.out.println ("Sort before =" + data[i]+ "");
}
Arrays.sort (Data,comparators.getcomparator ());
System.out.println ("————————————————————————————————————————————————————————————————————————");

for (int i = 0; i < data.length; i++) {
System.out.println ("after the sort =" + data[i]+ "");
}

}

}

Operation Effect:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.