第一种方法,就是list中对象实现Comparable接口,代码如下:
public class Person implements Comparable<Person> {
private String name;
private Integer order;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the order
*/
public Integer getOrder() {
return order;
}
/**
* @param order
* the order to set
*/
public void setOrder(Integer order) {
this.order = order;
}
@Override
public int compareTo(Person arg0) {
return this.getOrder().compareTo(arg0.getOrder());
}
}
View Code
第二种方法,就是在重载Collections.sort方法,代码如下:
public class Person {
private String name;
private Integer order;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the order
*/
public Integer getOrder() {
return order;
}
/**
* @param order
* the order to set
*/
public void setOrder(Integer order) {
this.order = order;
}
}
public static void main(String[] args) {
List<Person> listA = new ArrayList<Person>();
Person p1 = new Person();
Person p2 = new Person();
Person p3 = new Person();
p1.setName("name1");
p1.setOrder(1);
p2.setName("name2");
p2.setOrder(2);
p3.setName("name3");
p3.setOrder(3);
listA.add(p2);
listA.add(p1);
listA.add(p3);
Collections.sort(listA, new Comparator<Person>() {
public int compare(Person arg0, Person arg1) {
return arg0.getOrder().compareTo(arg1.getOrder());
}
});
for (Person p : listA) {
System.out.println(p.getName());
}
}
两次执行的结果都是:
name1
name2
name3
//首先比较年龄,如果年龄相同,则比较名字
int flag=user0.getAge().compareTo(user1.getAge());
if(flag==0){
return user0.getName().compareTo(user1.getName());
}else{
return flag;
}
from:http://www.cnblogs.com/wentiertong/archive/2011/03/07/1973698.html
public class Person implements Comparable<Person> {
private String name;
private Integer order;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the order
*/
public Integer getOrder() {
return order;
}
/**
* @param order
* the order to set
*/
public void setOrder(Integer order) {
this.order = order;
}
@Override
public int compareTo(Person arg0) {
return this.getOrder().compareTo(arg0.getOrder());
}
}
View Code
第二种方法,就是在重载Collections.sort方法,代码如下:
public class Person {
private String name;
private Integer order;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the order
*/
public Integer getOrder() {
return order;
}
/**
* @param order
* the order to set
*/
public void setOrder(Integer order) {
this.order = order;
}
}
public static void main(String[] args) {
List<Person> listA = new ArrayList<Person>();
Person p1 = new Person();
Person p2 = new Person();
Person p3 = new Person();
p1.setName("name1");
p1.setOrder(1);
p2.setName("name2");
p2.setOrder(2);
p3.setName("name3");
p3.setOrder(3);
listA.add(p2);
listA.add(p1);
listA.add(p3);
Collections.sort(listA, new Comparator<Person>() {
public int compare(Person arg0, Person arg1) {
return arg0.getOrder().compareTo(arg1.getOrder());
}
});
for (Person p : listA) {
System.out.println(p.getName());
}
}
两次执行的结果都是:
name1
name2
name3
//首先比较年龄,如果年龄相同,则比较名字
int flag=user0.getAge().compareTo(user1.getAge());
if(flag==0){
return user0.getName().compareTo(user1.getName());
}else{
return flag;
}
from:http://www.cnblogs.com/wentiertong/archive/2011/03/07/1973698.html