第一种方法,就是list中对象实现Comparable接口,代码如下:
01 public class Person implements Comparable<Person> {
02 private String name;
03 private Integer order;
04
05 /**
06 * @return the name
07 */
08 public String getName() {
09 return name;
10 }
11
12 /**
13 * @param name
14 * the name to set
15 */
16 public void setName(String name) {
17 this.name = name;
18 }
19
20 /**
21 * @return the order
22 */
23 public Integer getOrder() {
24 return order;
25 }
26
27 /**
28 * @param order
29 * the order to set
30 */
31 public void setOrder(Integer order) {
32 this.order = order;
33 }
34
35 @Override
36 public int compareTo(Person arg0) {
37 return this.getOrder().compareTo(arg0.getOrder());
38 }
39
40 }
01 public static void main(String[] args) {
02 List<Person> listA = new ArrayList<Person>();
03 Person p1 = new Person();
04 Person p2 = new Person();
05 Person p3 = new Person();
06
07 p1.setName("name1");
08 p1.setOrder(1);
09 p2.setName("name2");
10 p2.setOrder(2);
11 p3.setName("name3");
12 p3.setOrder(3);
13
14 listA.add(p2);
15 listA.add(p1);
16 listA.add(p3);
17 Collections.sort(listA);
18 for (Person p : listA) {
19 System.out.println(p.getName());
20 }
21 }
第二种方法,就是在重载Collections.sort方法,代码如下:
01 public class Person {
02 private String name;
03 private Integer order;
04
05 /**
06 * @return the name
07 */
08 public String getName() {
09 return name;
10 }
11
12 /**
13 * @param name
14 * the name to set
15 */
16 public void setName(String name) {
17 this.name = name;
18 }
19
20 /**
21 * @return the order
22 */
23 public Integer getOrder() {
24 return order;
25 }
26
27 /**
28 * @param order
29 * the order to set
30 */
31 public void setOrder(Integer order) {
32 this.order = order;
33 }
34
35 }
01 public static void main(String[] args) {
02 List<Person> listA = new ArrayList<Person>();
03 Person p1 = new Person();
04 Person p2 = new Person();
05 Person p3 = new Person();
06
07 p1.setName("name1");
08 p1.setOrder(1);
09 p2.setName("name2");
10 p2.setOrder(2);
11 p3.setName("name3");
12 p3.setOrder(3);
13
14 listA.add(p2);
15 listA.add(p1);
16 listA.add(p3);
17
18 Collections.sort(listA, new Comparator<Person>() {
19 public int compare(Person arg0, Person arg1) {
20 return arg0.getOrder().compareTo(arg1.getOrder());
21 }
22 });
23
24 for (Person p : listA) {
25 System.out.println(p.getName());
26 }
27 }
两次执行的结果都是:
name1
name2
name3
01 public class Person implements Comparable<Person> {
02 private String name;
03 private Integer order;
04
05 /**
06 * @return the name
07 */
08 public String getName() {
09 return name;
10 }
11
12 /**
13 * @param name
14 * the name to set
15 */
16 public void setName(String name) {
17 this.name = name;
18 }
19
20 /**
21 * @return the order
22 */
23 public Integer getOrder() {
24 return order;
25 }
26
27 /**
28 * @param order
29 * the order to set
30 */
31 public void setOrder(Integer order) {
32 this.order = order;
33 }
34
35 @Override
36 public int compareTo(Person arg0) {
37 return this.getOrder().compareTo(arg0.getOrder());
38 }
39
40 }
01 public static void main(String[] args) {
02 List<Person> listA = new ArrayList<Person>();
03 Person p1 = new Person();
04 Person p2 = new Person();
05 Person p3 = new Person();
06
07 p1.setName("name1");
08 p1.setOrder(1);
09 p2.setName("name2");
10 p2.setOrder(2);
11 p3.setName("name3");
12 p3.setOrder(3);
13
14 listA.add(p2);
15 listA.add(p1);
16 listA.add(p3);
17 Collections.sort(listA);
18 for (Person p : listA) {
19 System.out.println(p.getName());
20 }
21 }
第二种方法,就是在重载Collections.sort方法,代码如下:
01 public class Person {
02 private String name;
03 private Integer order;
04
05 /**
06 * @return the name
07 */
08 public String getName() {
09 return name;
10 }
11
12 /**
13 * @param name
14 * the name to set
15 */
16 public void setName(String name) {
17 this.name = name;
18 }
19
20 /**
21 * @return the order
22 */
23 public Integer getOrder() {
24 return order;
25 }
26
27 /**
28 * @param order
29 * the order to set
30 */
31 public void setOrder(Integer order) {
32 this.order = order;
33 }
34
35 }
01 public static void main(String[] args) {
02 List<Person> listA = new ArrayList<Person>();
03 Person p1 = new Person();
04 Person p2 = new Person();
05 Person p3 = new Person();
06
07 p1.setName("name1");
08 p1.setOrder(1);
09 p2.setName("name2");
10 p2.setOrder(2);
11 p3.setName("name3");
12 p3.setOrder(3);
13
14 listA.add(p2);
15 listA.add(p1);
16 listA.add(p3);
17
18 Collections.sort(listA, new Comparator<Person>() {
19 public int compare(Person arg0, Person arg1) {
20 return arg0.getOrder().compareTo(arg1.getOrder());
21 }
22 });
23
24 for (Person p : listA) {
25 System.out.println(p.getName());
26 }
27 }
两次执行的结果都是:
name1
name2
name3