要对集合中的对象的某属性进行排序有两种方式。
a. 一种是要排序对象类实现comparable接口的compareTo方法;然后把对象放入list;然后调用Collections.sort(list);
b. 一种是不对要排序对象类做任何改动,创建Comparator接口的实现类C;然后 把对象放入list;然后调用Collections.sort(list, C);
a.eg
----------------------------------
public class User implements Comparable<User>{
private String name;
private int age;
private int gender;
private String address;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
/**
* @return the gender
*/
public int getGender() {
return gender;
}
/**
* @param gender the gender to set
*/
public void setGender(int gender) {
this.gender = gender;
}
/**
* @return the address
*/
public String getAddress() {
return address;
}
/**
* @param address the address to set
*/
public void setAddress(String address) {
this.address = address;
}
/**
*
*/
public User() {
super();
}
/**
* @param name
* @param age
* @param gender
* @param address
*/
public User(String name, int age, int gender, String address) {
super();
this.name = name;
this.age = age;
this.gender = gender;
this.address = address;
}
@Override
public int compareTo(User o) {
if(o!=null){
if(this.getAge()>o.getAge()){
return 1;
}else if(this.getAge()==o.getAge()){
return 0;
}
}
return -1;
}
}
-------------------------------------------main
List<User> ulist = new ArrayList<User>();
ulist.add(new User("wangbo",29,1,"长沙"));
ulist.add(new User("wumei",44,1,"株洲"));
ulist.add(new User("zhangjie",19,1,"岳阳"));
ulist.add(new User("lihua",36,1,"长沙"));
ulist.add(new User("zhangchangzhe",19,1,"衡阳"));
Collections.sort(list);
for(User u:ulist){
System.out.println(u.getName()+"\t"+u.getAge()+"\t"+u.getAddress());
}
==============================================
b.eg
--------------------------------
public class User {
private String name;
private int age;
private int gender;
private String address;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
/**
* @return the gender
*/
public int getGender() {
return gender;
}
/**
* @param gender the gender to set
*/
public void setGender(int gender) {
this.gender = gender;
}
/**
* @return the address
*/
public String getAddress() {
return address;
}
/**
* @param address the address to set
*/
public void setAddress(String address) {
this.address = address;
}
/**
*
*/
public User() {
super();
}
/**
* @param name
* @param age
* @param gender
* @param address
*/
public User(String name, int age, int gender, String address) {
super();
this.name = name;
this.age = age;
this.gender = gender;
this.address = address;
}
}
------------------------------
import java.util.Comparator;
public class ComparatorImpl implements Comparator<User>{
@Override
public int compare(User o1, User o2) {
if(null!=o1 && null!=o2){
if(o1.getAge() >o2.getAge()){
return 1;
}else if(o1.getAge() ==o2.getAge()){
return 0;
}
}
return -1;
}
}
-----------------------------------main
List<User> ulist = new ArrayList<User>();
ulist.add(new User("wangbo",29,1,"长沙"));
ulist.add(new User("wumei",44,1,"株洲"));
ulist.add(new User("zhangjie",19,1,"岳阳"));
ulist.add(new User("lihua",36,1,"长沙"));
ulist.add(new User("zhangchangzhe",19,1,"衡阳"));
Collections.sort(ulist, new ComparatorImpl());
for(User u:ulist){
System.out.println(u.getName()+"\t"+u.getAge()+"\t"+u.getAddress());
}