【问题标题】:Java String date comparatorJava 字符串日期比较器
【发布时间】: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


    【解决方案1】:
    Collections.sort(channelsList, new Comparator<Channel>() {
        DateFormat format = new SimpleDateFormat(DATE_FORMAT_PATTERN);
    
        @Override
        public int compare(Channel o1, Channel o2) {
            // If both are null, they are equal
            if (o1.getConversation() == null && o2.getConversation() == null)
                return 0;
    
            // If only first one is null, it is less than the other (null's come first)
            if (o1.getConversation() == null)
                return -1;
    
            // If only second one is null, it is greater than the other
            if (o2.getConversation() == null)
                return 1;
    
            Conversation c1 = o1.getConversation();
            Conversation c2 = o2.getConversation();
    
            // Same comparisons are done here again
            if (c1.getSentAt() == null && c2.getSentAt() == null)
                return 0;
    
            if (c1.getSentAt() == null)
                return -1;
    
            if (c2.getSentAt() == null)
                return 1;
    
            try {
                if (Util.isUTCFormat(o1.getConversation().getSentAt()) && 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);
            }              
        }
    });
    

    【讨论】:

    • @androidbash 然后定义它们应该如何排序并实现它 - 如果对象相等,则它们的排序顺序无关紧要。如果您想添加超出您定义的标准的结构,请执行此操作。
    【解决方案2】:

    您可以使用比较器或可比较来对数组进行排序

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-14
      • 2012-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多