要对集合中的对象的某属性进行排序有两种方式。
a. 一种是要排序对象类实现comparable接口的compareTo方法;然后把对象放入list;然后调用Collections.sort(list);
b. 一种是不对要排序对象类做任何改动,创建Comparator接口的实现类C;然后 把对象放入list;然后调用Collections.sort(list, C);
在对象中定义排序
对象中数值型排序
public class User implements Comparable<User>{
private String name;
private int age;
private int gender;
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getGender() {
return gender;
}
public void setGender(int gender) {
this.gender = gender;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public User() {
super();
}
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());
}
不该动对象类
public class User {
private String name;
private int age;
private int gender;
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getGender() {
return gender;
}
public void setGender(int gender) {
this.gender = gender;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public User() {
super();
}
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());
}
对象中字符串排序
public class HahaTime2 {
public static void main(String[] args) {
/**
* 原来的日期
*/
List<Person> list = new ArrayList<Person>();
list.add(new Person(1,"2016-04-22 15:30:28", "小芳"));
list.add(new Person(1,"2016-03-02 15:30:28", "小花"));
list.add(new Person(1,"2016-02-12 15:30:28", "小狗"));
list.add(new Person(1,"2016-12-02 15:30:28", "小猫"));
list.add(new Person(1,"2016-02-02 15:30:28", "小米"));
list.add(new Person(1,"2016-02-12 15:30:28", "小明"));
Collections.sort(list, new MapComparator());
for (Person person : list) {
System.out.println(person.name+";"+person.beginTime);
}
}
}
class MapComparator implements Comparator<Person>{
public int compare(Person lhs, Person rhs) {
return lhs.beginTime.compareTo(rhs.beginTime);
}
}