java对象比较器_使用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

posted on 2008-04-16 20:47 周锐 阅读(2681) 评论(5)  编辑  收藏 所属分类: Java

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值