【发布时间】:2018-03-30 10:01:05
【问题描述】:
我有一个频道列表,名为 channelsList。
List<Channel> channelsList;
频道类
public class Channel{
public Conversation conversation;
}
会话课
public class Conversation{
public String sentAt;
}
我需要按 myDate 的降序对频道列表进行排序。我该如何使用比较器呢?到目前为止我试过这个。但这不起作用,因为我的集合 Channel 可以有 null 的对话值,而 Conversation 可以有 null 的 sentAt 值。任何帮助将不胜感激。
Collections.sort(channelsList, new Comparator<Channel>() {
DateFormat format = new SimpleDateFormat(DATE_FORMAT_PATTERN);
@Override
public int compare(Channel o1, Channel o2) {
try {
if (o1.getConversation() != null && Util.isUTCFormat(o1.getConversation().getSentAt()) && o2.getConversation() != null && Util.isUTCFormat(o2.getConversation().getSentAt()))
return format.parse(o1.getConversation().getSentAt()).compareTo(format.parse(o2.getConversation().getSentAt()));
else
return -1;
} catch (ParseException e) {
throw new IllegalArgumentException(e);
}
}
});
【问题讨论】:
标签: java android comparator