使用JAVA对象比较器Comparator

使用JAVA对象比较器Comparator
需求:公司要将员工进行排序(不要说领导排在前面),我们的需求比较复杂。先进行姓排序,谁的姓拼音靠前,谁就排前面。然后对名字进行排序。恩.如果同名,女性排前头。如果名字和性别都相同,年龄小的排前头。
代码如下:

Person.java

package com.founder.common;

public class Person {
 private String firstName;
 private String lastName;
 private boolean sex;
 private int age;

 public Person(String firstName, String lastName, boolean sex, int age) {
  this.firstName = firstName;
  this.lastName = lastName;
  this.sex = sex;
  this.age = age;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int 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 boolean getSex() {
  return sex;
 }
 public void setSex(boolean sex) {
  this.sex = sex;
 }
 
 public String toString() {
  return firstName + " " + lastName + " " + (sex ? "Male" : "Female") + " " + age;
 }
}

Comparator.java

package com.founder.common;

public class Comparator{
 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("未找到适合的比较器");
     return 1;
    }
   }
   
   public int compare(String o1, String o2) {
    int len1 = o1.length();
    int len2 = o2.length();
    int n = Math.min(len1, len2);
    char v1[] = o1.toCharArray();
    char v2[] = o2.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();  
          return (val1 < val2 ? -1 : (val1 == val2 ? 0 : 1));  
      }
   
   public int compare(boolean o1, boolean o2) {
    return (o1 == o2 ? 0 : (o1 == true ? 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();  
          boolean sex1 = o1.getSex();  
          boolean sex2 = o2.getSex();  
          int age1 = o1.getAge();  
          int age2 = o2.getAge();  
          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));
   }
  };
 }
}

PersonTest.java

package com.founder.common;

public class PersonTest {

 public static void main(String[] args) {
  Person[] person = new Person[] {
    new Person("ouyang", "feng", true, 25),
    new Person("zhuang", "gw", false, 36),
    new Person("zhuang", "gw", true, 36),
    new Person("zhuang", "gw", true, 33),
    new Person("zhuang", "gw", false, 23),
    new Person("yang", "gw", true, 43)};
  for (int i = 0; i < person.length; i++) {
   System.out.println("before sort=" + person[i]);
  }

  java.util.Arrays.sort(person, Comparator.getComparator());

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

 }

}


输出结果为:
before sort=ouyang feng Male 25
before sort=zhuang gw Female 36
before sort=zhuang gw Male 36
before sort=zhuang gw Male 33
before sort=zhuang gw Female 23
before sort=yang gw Male 43
*************************************
after sort=ouyang feng Male 25
after sort=yang gw Male 43
after sort=zhuang gw Female 23
after sort=zhuang gw Female 36
after sort=zhuang gw Male 33
after sort=zhuang gw Male 36

你可能感兴趣的:(使用JAVA对象比较器Comparator)