Java根据容器(List,ArrayList)内对象的字段排序

用例是这样的:
一个结果集List,里面有很多的对象,要求根据对象的字段进行排序。

算法:
1. 对象实现Comparable接口,并从写compareTo()方法,方法里是排序规则。
2. 利用Arrays.sort(o)进行排序,不要要注意需要List.toArray();
3. 获得排序后的List。

具体算法如下:

import java.util.Date;

/**
* 需要排序的bean对象。
*
* @author zaki
*
*/
public class ComparableBean implements Comparable {

private int id;

private String name;

private Date birthday;

public Date getBirthday() {
return birthday;
}

public void setBirthday(Date birthday) {
this.birthday = birthday;
}

/*
* 按照id从大到到小的顺序排序。 自己可以在方法里面编写任意的排序算法。
*/
public int compareTo(ComparableBean o) {
// 根据名字从小到大顺序:if(String.CASE_INSENSITIVE_ORDER.compare(getName(), o.getName()) > 0)
// 根据生日从小到大顺序:if (this.birthday.compareTo(o.getBirthday()) > 0)
// 根据ID从小到大顺序。
if(this.getId() > o.getId()){
return 1;
}else {
return -1;
}
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String toString() {
return getId() + " : " + getName() + " : " + getBirthday().toString();
}
}


排序和测试类:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Random;

public class TestComparable {

private TestComparable() {
ComparableBean cbean;
Random rand = new Random();
List beans = new ArrayList();
for (int i = 0; i < 100; i++) {
cbean = new ComparableBean();
cbean.setId(rand.nextInt(10000));
cbean.setBirthday(new Date());
cbean.setName("TestName" + rand.nextInt(1000));
beans.add(cbean);
}

Object[] beanObjects = beans.toArray();
Arrays.sort(beanObjects);

beans.removeAll(beans);
System.out.println(beans.size());

for (int i = 0; i < 100; i++) {
System.out.println(beanObjects[i]);
}

System.out.println("ok");
}

public static void main(String[] args) {
new TestComparable();
}

}



运行结果如下:

0
181 : TestName290 : Tue Mar 29 15:51:54 CST 2011
230 : TestName45 : Tue Mar 29 15:51:54 CST 2011
275 : TestName601 : Tue Mar 29 15:51:54 CST 2011
354 : TestName689 : Tue Mar 29 15:51:54 CST 2011
657 : TestName755 : Tue Mar 29 15:51:54 CST 2011
792 : TestName471 : Tue Mar 29 15:51:54 CST 2011
1041 : TestName441 : Tue Mar 29 15:51:54 CST 2011
1061 : TestName821 : Tue Mar 29 15:51:54 CST 2011
1247 : TestName836 : Tue Mar 29 15:51:54 CST 2011
1347 : TestName139 : Tue Mar 29 15:51:54 CST 2011
1791 : TestName105 : Tue Mar 29 15:51:54 CST 2011
1882 : TestName930 : Tue Mar 29 15:51:54 CST 2011
1993 : TestName969 : Tue Mar 29 15:51:54 CST 2011
2114 : TestName912 : Tue Mar 29 15:51:54 CST 2011
2131 : TestName677 : Tue Mar 29 15:51:54 CST 2011
2168 : TestName975 : Tue Mar 29 15:51:54 CST 2011
2215 : TestName354 : Tue Mar 29 15:51:54 CST 2011
2253 : TestName84 : Tue Mar 29 15:51:54 CST 2011
2310 : TestName928 : Tue Mar 29 15:51:54 CST 2011
2385 : TestName466 : Tue Mar 29 15:51:54 CST 2011
2387 : TestName492 : Tue Mar 29 15:51:54 CST 2011
2388 : TestName416 : Tue Mar 29 15:51:54 CST 2011
2578 : TestName873 : Tue Mar 29 15:51:54 CST 2011
2603 : TestName8 : Tue Mar 29 15:51:54 CST 2011
2768 : TestName538 : Tue Mar 29 15:51:54 CST 2011
2790 : TestName503 : Tue Mar 29 15:51:54 CST 2011
2797 : TestName884 : Tue Mar 29 15:51:54 CST 2011
2893 : TestName747 : Tue Mar 29 15:51:54 CST 2011
2894 : TestName333 : Tue Mar 29 15:51:54 CST 2011
2961 : TestName33 : Tue Mar 29 15:51:54 CST 2011
3086 : TestName691 : Tue Mar 29 15:51:54 CST 2011
3156 : TestName70 : Tue Mar 29 15:51:54 CST 2011
3220 : TestName534 : Tue Mar 29 15:51:54 CST 2011
3274 : TestName220 : Tue Mar 29 15:51:54 CST 2011
3361 : TestName696 : Tue Mar 29 15:51:54 CST 2011
3578 : TestName709 : Tue Mar 29 15:51:54 CST 2011
3593 : TestName982 : Tue Mar 29 15:51:54 CST 2011
3751 : TestName619 : Tue Mar 29 15:51:54 CST 2011
3760 : TestName707 : Tue Mar 29 15:51:54 CST 2011
3989 : TestName241 : Tue Mar 29 15:51:54 CST 2011
4189 : TestName900 : Tue Mar 29 15:51:54 CST 2011
4321 : TestName442 : Tue Mar 29 15:51:54 CST 2011
4460 : TestName739 : Tue Mar 29 15:51:54 CST 2011
4566 : TestName251 : Tue Mar 29 15:51:54 CST 2011
4687 : TestName261 : Tue Mar 29 15:51:54 CST 2011
4809 : TestName138 : Tue Mar 29 15:51:54 CST 2011
4933 : TestName168 : Tue Mar 29 15:51:54 CST 2011
4939 : TestName298 : Tue Mar 29 15:51:54 CST 2011
4944 : TestName885 : Tue Mar 29 15:51:54 CST 2011
5000 : TestName299 : Tue Mar 29 15:51:54 CST 2011
5116 : TestName528 : Tue Mar 29 15:51:54 CST 2011
5261 : TestName239 : Tue Mar 29 15:51:54 CST 2011
5330 : TestName665 : Tue Mar 29 15:51:54 CST 2011
5530 : TestName719 : Tue Mar 29 15:51:54 CST 2011
5543 : TestName273 : Tue Mar 29 15:51:54 CST 2011
5599 : TestName389 : Tue Mar 29 15:51:54 CST 2011
5762 : TestName132 : Tue Mar 29 15:51:54 CST 2011
5798 : TestName156 : Tue Mar 29 15:51:54 CST 2011
5988 : TestName507 : Tue Mar 29 15:51:54 CST 2011
6049 : TestName569 : Tue Mar 29 15:51:54 CST 2011
6092 : TestName671 : Tue Mar 29 15:51:54 CST 2011
6135 : TestName403 : Tue Mar 29 15:51:54 CST 2011
6226 : TestName492 : Tue Mar 29 15:51:54 CST 2011
6251 : TestName397 : Tue Mar 29 15:51:54 CST 2011
6310 : TestName892 : Tue Mar 29 15:51:54 CST 2011
6434 : TestName254 : Tue Mar 29 15:51:54 CST 2011
6622 : TestName436 : Tue Mar 29 15:51:54 CST 2011
6631 : TestName609 : Tue Mar 29 15:51:54 CST 2011
6676 : TestName348 : Tue Mar 29 15:51:54 CST 2011
6856 : TestName309 : Tue Mar 29 15:51:54 CST 2011
6906 : TestName492 : Tue Mar 29 15:51:54 CST 2011
6944 : TestName812 : Tue Mar 29 15:51:54 CST 2011
6990 : TestName563 : Tue Mar 29 15:51:54 CST 2011
7235 : TestName67 : Tue Mar 29 15:51:54 CST 2011
7275 : TestName783 : Tue Mar 29 15:51:54 CST 2011
7625 : TestName457 : Tue Mar 29 15:51:54 CST 2011
7670 : TestName598 : Tue Mar 29 15:51:54 CST 2011
7687 : TestName813 : Tue Mar 29 15:51:54 CST 2011
7884 : TestName581 : Tue Mar 29 15:51:54 CST 2011
7945 : TestName350 : Tue Mar 29 15:51:54 CST 2011
8062 : TestName772 : Tue Mar 29 15:51:54 CST 2011
8090 : TestName971 : Tue Mar 29 15:51:54 CST 2011
8107 : TestName368 : Tue Mar 29 15:51:54 CST 2011
8283 : TestName516 : Tue Mar 29 15:51:54 CST 2011
8322 : TestName434 : Tue Mar 29 15:51:54 CST 2011
8332 : TestName960 : Tue Mar 29 15:51:54 CST 2011
8933 : TestName326 : Tue Mar 29 15:51:54 CST 2011
8952 : TestName818 : Tue Mar 29 15:51:54 CST 2011
9051 : TestName363 : Tue Mar 29 15:51:54 CST 2011
9062 : TestName551 : Tue Mar 29 15:51:54 CST 2011
9099 : TestName913 : Tue Mar 29 15:51:54 CST 2011
9204 : TestName266 : Tue Mar 29 15:51:54 CST 2011
9313 : TestName555 : Tue Mar 29 15:51:54 CST 2011
9526 : TestName234 : Tue Mar 29 15:51:54 CST 2011
9573 : TestName948 : Tue Mar 29 15:51:54 CST 2011
9575 : TestName284 : Tue Mar 29 15:51:54 CST 2011
9635 : TestName409 : Tue Mar 29 15:51:54 CST 2011
9762 : TestName963 : Tue Mar 29 15:51:54 CST 2011
9819 : TestName962 : Tue Mar 29 15:51:54 CST 2011
9995 : TestName116 : Tue Mar 29 15:51:54 CST 2011
ok

你可能感兴趣的:(杂项)