# 冒泡排序法 依据实体中的某个字段对实体 进行排序

www.MyException.Cn  网友分享于：2013-09-28  浏览：0次

``` 1 package com.kns.test;
2
3 import java.util.Date;
4
5 public class testClass {
6     private String name;
7     private Integer age;
8     private Date date;
9
10     public testClass(String name, Integer age, Date date) {
11         this.name = name;
12         this.age = age;
13         this.date = date;
14     }
15
16     public String getName() {
17         return name;
18     }
19
20     public void setName(String name) {
21         this.name = name;
22     }
23
24     public Integer getAge() {
25         return age;
26     }
27
28     public void setAge(Integer age) {
29         this.age = age;
30     }
31
32     public Date getDate() {
33         return date;
34     }
35
36     public void setDate(Date date) {
37         this.date = date;
38     }
39 }```

``` 1 package com.kns.test;
2
3 import java.util.ArrayList;
4 import java.util.Date;
5 import java.util.List;
6
7 public class demo3 {
8
9     public static void main(String[] args) {
10         testClass a = new testClass("shaojiajia",1, new Date());
11         testClass b = new testClass("shaojiajia",17, new Date());
12         testClass c = new testClass("shaojiajia",11, new Date());
13         testClass d = new testClass("shaojiajia",19, new Date());
14         testClass e = new testClass("shaojiajia",12, new Date());
15         List<testClass> testClassList = new ArrayList<testClass>();
21         testClassList.toArray();
22
23         Object[] testClasses = testClassList.toArray();
24         for (Object object : testClasses) {
25             testClass testClass = (testClass) object;
26             System.out.println(testClass.getAge());
27         }
28
29         System.out.println("--------------------------------------");
30
31         Object temp;
32         for (int i = 0; i < testClasses.length - 1; i++) {
33             for (int j = i+1; j < testClasses.length; j++) {
34                 if (((testClass)testClasses[i]).getAge() < ((testClass)testClasses[j]).getAge()) {
35                     temp = testClasses[j];
36                     testClasses[j] = testClasses[i];
37                     testClasses[i] = temp;
38                 }
39             }
40         }
41         for (Object object : testClasses) {
42             testClass testClass = (testClass) object;
43             System.out.println(testClass.getAge());
44         }
45     }
46 }```