Java custom ArrayList sorting, skipped

Source: Internet
Author: User

Java custom ArrayList sorting, skipped

There are two types of objects in a list. Now there is a need to sort the dates of different objects. below is the code

/**
* @ Author Xiaohua xh1991101@163.com
*/

List Messages = messageSrv. getList (u. getUserId ());

List MessageList = new ArrayList();
For (Object o: messages ){
MessageList. add (o );
}
For (Object o: checks ){
MessageList. add (o );
}
Collections. sort (messageList, new Comparator (){
Public int compare (Object o1, Object o2 ){
If (o1 instanceof Message & o2 instanceof Message ){
Message m = (Message) o1;
Message m2 = (Message) o2;
If (m. getMessageDate (). after (m2.getMessageDate ()))
Return 1;
If (m. getMessageDate (). equals (m2.getMessageDate ()))
Return 0;
If (m. getMessageDate (). before (m2.getMessageDate ()))
Return-1;
} Else if (o1 instanceof CheckRecord & o2 instanceof Message ){
CheckRecord m = (CheckRecord) o1;
Message m2 = (Message) o2;
If (m. getCdate (). after (m2.getMessageDate ()))
Return 1;
If (m. getCdate (). equals (m2.getMessageDate ()))
Return 0;
If (m. getCdate (). before (m2.getMessageDate ()))
Return-1;
} Else if (o1 instanceof CheckRecord & o2 instanceof CheckRecord ){
CheckRecord m = (CheckRecord) o1;
CheckRecord m2 = (CheckRecord) o2;
If (m. getCdate (). after (m2.getCdate ()))
Return 1;
If (m. getCdate (). equals (m2.getCdate ()))
Return 0;
If (m. getCdate (). before (m2.getCdate ()))
Return-1;

} Else if (o1 instanceof Message & o2 instanceof CheckRecord ){
Message m = (Message) o1;
CheckRecord m2 = (CheckRecord) o2;
If (m. getMessageDate (). after (m2.getCdate ()))
Return 1;
If (m. getMessageDate (). equals (m2.getCdate ()))
Return 0;
If (m. getMessageDate (). before (m2.getCdate ()))
Return-1;
}
Return 0;

}


It is not easy to write a log, reprinted please indicate the source: http://blog.csdn.net/xh199110 Thank you

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.