Want to order chats in ArrayList by timestamp using Comparator, but it's not working and I don't know why

前端 未结 2 1805
别那么骄傲
别那么骄傲 2021-01-21 17:55

I have implemented a chat function in my app and it\'s working fine, only problem is that now I want the chats to be ordered by timestamp, so that the newest ones c

2条回答
  •  暖寄归人
    2021-01-21 18:35

    You are sorting collection in ascending order by using Long.compare(). For showing latest chat you need to change the compare method as

    public int compare(Chatlist o1, Chatlist o2) {
                        return o1.getTimestamp() < o2.getTimestamp() ? 1 : 
                       (o1.getTimestamp == o2.getTimestamp() ? 0 : -1);
    }
    

提交回复
热议问题