java提供了一个类可以方便的进行对象的排序问题,里面用到的类还是上一篇博客中使用的MessageBase,MessageMms,MessageSms
定义一个Comparator,用来说明对象是依据哪个参数进行比较的,
public class ComparatorMessage implements Comparator {
public int compare(Object arg0, Object arg1) {
MessageBase message0 = (MessageBase) arg0;
MessageBase message1 = (MessageBase) arg1;
int flag = ((Long) message0.mDate).compareTo((Long) message1.mDate);
return flag;
}
}
进行比较:
Collections.sort(mMessageList, new ComparatorMessage());
排序后mMessageList就是依据mDate排好的列表,真心很方便~~