问题描述
有人可以帮我吗?我发现的每个例子都是按字母顺序排列,而我需要按日期排序的元素。我的ArrayList包含其中一个数据组件是一个DateTime对象的对象。在DateTime我可以调用函数:
lt()// less-than
lteq()// less -than-or-equal-to
所以要比较我可以做一些像:
if(myList.get(i).lt(myList.get(j))){
// ...
}
我不知道在if块中该怎么做。任何想法?
您可以使对象相当:
public static class MyObject implements Comparable< MyObject> {
private Date dateTime;
public Date getDateTime(){
return dateTime;
}
public void setDateTime(Date datetime){
this.dateTime = datetime;
}
@Override
public int compareTo(MyObject o){
return getDateTime()。compareTo(o.getDateTime());
}
}
然后通过调用排序:
Collections.sort(myList);
但有时您不想更改模型,就像当您要对几种不同的排序属性。在这种情况下,您可以即时创建比较器:
Collections.sort(myList,new Comparator< MyObject>()
public int compare(MyObject o1,MyObject o2){
return o1.getDateTime()。compareTo(o2.getDateTime());
}
});
但是,只有当您确定dateTime在比较时不为null 。处理null也是明智的,以避免NullPointerExceptions:
public static class MyObject implements Comparable< MyObject> {
private Date dateTime;
public Date getDateTime(){
return dateTime;
}
public void setDateTime(Date datetime){
this.dateTime = datetime;
}
@Override
public int compareTo(MyObject o){
if(getDateTime()== null || o.getDateTime()== null)
return 0;
return getDateTime()。compareTo(o.getDateTime());
}
}
或者在第二个例子中:
Collections.sort(myList,new Comparator< MyObject>(){
public int compare(MyObject o1,MyObject o2){
if(o1.getDateTime()== null || o2.getDateTime()== null)
return 0;
return o1.getDateTime()。compareTo(o2.getDateTime());
}
});
Can someone help me with this? Every example I find is about doing this alphabetically, while I need my elements sorted by date.
My ArrayList contains objects on which one of the datamembers is a DateTime object. On DateTime I can call the functions:
lt() // less-than
lteq() // less-than-or-equal-to
So to compare I could do something like:
if(myList.get(i).lt(myList.get(j))){
// ...
}
I don't really know what to do inside the if block. Any ideas?
You can make your object comparable:
public static class MyObject implements Comparable<MyObject> {
private Date dateTime;
public Date getDateTime() {
return dateTime;
}
public void setDateTime(Date datetime) {
this.dateTime = datetime;
}
@Override
public int compareTo(MyObject o) {
return getDateTime().compareTo(o.getDateTime());
}
}
And then you sort it by calling:
Collections.sort(myList);
However sometimes you don't want to change your model, like when you want to sort on several different properties. In that case, you can create comparator on the fly:
Collections.sort(myList, new Comparator<MyObject>() {
public int compare(MyObject o1, MyObject o2) {
return o1.getDateTime().compareTo(o2.getDateTime());
}
});
However, the above works only if you're certain that dateTime is not null at the time of comparison. It's wise to handle null as well to avoid NullPointerExceptions:
public static class MyObject implements Comparable<MyObject> {
private Date dateTime;
public Date getDateTime() {
return dateTime;
}
public void setDateTime(Date datetime) {
this.dateTime = datetime;
}
@Override
public int compareTo(MyObject o) {
if (getDateTime() == null || o.getDateTime() == null)
return 0;
return getDateTime().compareTo(o.getDateTime());
}
}
Or in the second example:
Collections.sort(myList, new Comparator<MyObject>() {
public int compare(MyObject o1, MyObject o2) {
if (o1.getDateTime() == null || o2.getDateTime() == null)
return 0;
return o1.getDateTime().compareTo(o2.getDateTime());
}
});
这篇关于按日期排序ArrayList中的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!