list排序问题

bluemouse_2008 2010-10-27 03:15:56
查询结果的list里面放了一系列的对象,对象里面有属性,现在要求将list查询出来后
就根据对象的某个属性排序(如对象的年龄降序排),list有这样的排序方法吗?求
解决方法
...全文
135 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
bluemouse_2008 2010-10-27
  • 打赏
  • 举报
回复
3楼给的很详细,谢谢了!
mythling 2010-10-27
  • 打赏
  • 举报
回复
晕,还不是和我的实现一样,不看也罢!!!!
blazingfire 2010-10-27
  • 打赏
  • 举报
回复
示例按birthday属性排序

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

public class Test {

public static class Man {

private String birthday;
private String name;

public Man() {
}

public Man(String name, String birthday) {
this.name = name;
this.birthday = birthday;
}

@Override
public String toString() {
return name + " " + birthday;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getBirthday() {
return birthday;
}

public void setBirthday(String birthday) {
this.birthday = birthday;
}
}

public static void main(String[] args) {
List<Man> list = new ArrayList<Man>();
list.add(new Man("张三", "2000-01-01 20:20:20"));
list.add(new Man("李四", "2001-01-01 20:20:20"));
list.add(new Man("王五", "2005-05-01 20:20:20"));
list.add(new Man("BBB", "2003-01-01 20:20:20"));

System.out.println("排序前:" + list);
Collections.sort(list, new Comparator<Man>() {

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

private Date getBirthday(Man m) {
if (m == null || m.getBirthday() == null)
return null;
try {
return sdf.parse(m.getBirthday());
} catch (ParseException e) {
return null;
}
}

public int compare(Man o1, Man o2) {
Date d1 = getBirthday(o1);
Date d2 = getBirthday(o2);
if (d1 == null && d2 == null)
return 0;

if (d1 == null)
return -1;
if (d2 == null)
return 1;
return d1.compareTo(d2);
}

});
System.out.println("排序后:" + list);
}

}
closewbq 2010-10-27
  • 打赏
  • 举报
回复

Collections.sort(list, new Comparator<对象>(){
public int compare(对象 o1, 对象 o2)
{
return o1.getAge()>o2.getAge()?1 :(o1.getAge()<o2.getAge()?-1:0);
}
});
宁波朱超 2010-10-27
  • 打赏
  • 举报
回复
[Quote=引用楼主 bluemouse_2008 的回复:]
查询结果的list里面放了一系列的对象,对象里面有属性,现在要求将list查询出来后
就根据对象的某个属性排序(如对象的年龄降序排),list有这样的排序方法吗?求
解决方法
[/Quote]

請用 COLLECTIONS
孤独剑客 2010-10-27
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wy100304441 的回复:]
http://www.blogjava.net/zygcs/archive/2008/01/17/176032.html
[/Quote]

这个很好!
woainiyibangcui 2010-10-27
  • 打赏
  • 举报
回复
用SortedSet吧 bean实现一下Comparator接口就行了 想怎么排怎么排
mythling 2010-10-27
  • 打赏
  • 举报
回复

departmentList = departmentService.findAll();
Collections.sort(departmentList, new CompartorDepartment());

public class CompartorDepartment extends Department implements Comparator
{
private static final long serialVersionUID = 1L;

public CompartorDepartment()
{

}

public int compare(Object a, Object b)
{
Department s1 = (Department) a;
Department s2 = (Department) b;
return s1.getId() - s2.getId() < 0 ? -1 : 1;

}
}

mythling 2010-10-27
  • 打赏
  • 举报
回复
有啊。呵呵

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧