1，3，5，8，3，6

1，3，3，5，6，8

a,i,e,f,w,s

a,e,f,i,s,w

int compare(Object o1, Object o2) 返回一个基本类型的整型，返回负数表示o1 小于o2，返回0 表示o1和o2相等，返回正数表示o1大于o2。

public 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;
}

//为了输入方便，重写了toString()

public String toString()
{
return firstname +" "+lastname+" "+(sex.booleanValue()?"男":"女")+" "+age;
}
}
//end person

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("未找到合适的比较器");
return 1;

}
}

public int compare(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;
}

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.equals(o2)? 0 : (o1.booleanValue()==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();
Integer age1 = o1.getAge();
Integer 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));

}

};
}

}

compare(Person o1, Person o2)的返回值看起来比较别扭。最简单的是

public int compare(Boolean o1, Boolean o2) {

return (o1.equals(o2)? 0 : (o1.booleanValue()==true?1:-1));

}

o1和o2相等返回0，否则o1如果是true 就表示o1大于o2。

public class Main {
public Main() {
}
public static void main(String[] args) {
Person[] person = new Person[] {
new Person("ouyang", "feng", Boolean.TRUE, new Integer(27)),
new Person("zhuang", "gw", Boolean.TRUE, new Integer(27)),
new Person("zhuang", "gw", Boolean.FALSE, new Integer(27)),
new text.Person("zhuang", "gw", Boolean.FALSE, new Integer(2)),

};
for (int i = 0; i < person.length; i++) {
System.out.println("before sort=" + person[i]);
}
java.util.Arrays.sort(person, Comparators.getComparator());
for (int i = 0; i < person.length; i++) {
System.out.println("after sort=" + person[i]);
}

}

}

before sort=ouyang feng 男 27

before sort=zhuang gw 男 27

before sort=zhuang gw 女 27

before sort=zhuang gw 女 2

after sort=ouyang feng 男 27

after sort=zhuang gw 女 2

after sort=zhuang gw 女 27

after sort=zhuang gw 男 27

posted on 2006-05-27 17:20 nake 阅读(2184) 评论(5)  编辑  收藏

FeedBack:
# re: 比较器Comparator
2006-05-28 04:14 | 潜心

# re: 比较器Comparator
2006-05-30 18:10 | 贝隆夫人

package example.sort;

import org.apache.commons.lang.builder.CompareToBuilder;

public class Person implements Comparable
{
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;
}

// 为了输入方便，重写了toString()

public String toString()
{
return firstname + " " + lastname + " " + (sex.booleanValue() ? "男" : "女") + " " + age;
}

/**
* @see java.lang.Comparable#compareTo(Object)
*/
public int compareTo(Object object)
{
Person myClass = (Person) object;
return new CompareToBuilder().append(this.lastname, myClass.lastname).append(this.firstname, myClass.firstname).append(this.sex, myClass.sex).append(this.age, myClass.age).toComparison();
}
}

package example.sort;

public class Main
{
public Main()
{
}

public static void main(String[] args)
{
Person[] person = new Person[] { new Person("ouyang", "feng", Boolean.TRUE, new Integer(27)), new Person("zhuang", "gw", Boolean.TRUE, new Integer(27)),
new Person("zhuang", "gw", Boolean.FALSE, new Integer(27)), new Person("zhuang", "gw", Boolean.FALSE, new Integer(2)),

};
for (int i = 0; i < person.length; i++)
{
System.out.println("before sort=" + person[i]);
}
java.util.Arrays.sort(person);
for (int i = 0; i < person.length; i++)
{
System.out.println("after sort=" + person[i]);
}
}
}
回复  更多评论

# re: 比较器Comparator
2006-06-05 14:38 | tohu
hehe,不错，简与繁，知其然知其所以然，good，

# re: 比较器Comparator[未登录]
2008-06-17 00:18 | a

# re: 比较器Comparator[未登录]
2010-09-28 13:16 | 天行者

 只有注册用户登录后才能发表评论。 网站导航:
 < 2006年5月 >
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

•

• 积分 - 446724
• 排名 - 111