Sorting collections in Java

Source: Internet
Author: User

1. Defining sorting

classComparatordefaultImplementsComparator { Public intCompare (Object arg0, Object arg1) {orderdetailreturn.bidsentity bid0=(orderdetailreturn.bidsentity) arg0; Orderdetailreturn.bidsentity Bid1=(orderdetailreturn.bidsentity) arg1; //Sort by ID            returnBid0.getid ()-Bid1.getid (); }    }    classComparatorcreditImplementsComparator { Public intCompare (Object arg0, Object arg1) {orderdetailreturn.bidsentity bid0=(orderdetailreturn.bidsentity) arg0; Orderdetailreturn.bidsentity Bid1=(orderdetailreturn.bidsentity) arg1; //Sort by ID            if(Bid0.getbearer () = =NULL|| Bid1.getbearer () = =NULL)return0; returnBid0.getbearer (). Getscore ()-bid1.getbearer (). Getscore (); }    }    classComparatorpriceImplementsComparator { Public intCompare (Object arg0, Object arg1) {orderdetailreturn.bidsentity bid0=(orderdetailreturn.bidsentity) arg0; Orderdetailreturn.bidsentity Bid1=(orderdetailreturn.bidsentity) arg1; //Sort by ID            returnBid0.getprice ()-Bid1.getprice (); }    }

2. Call sort

if NULL return ;        Comparator Comparator;         if (style = = 1)            {new  comparatorcredit ()        ; Else if (style = = 2)            {new  comparatorprice ()        ; Else {            new  comparatordefault ();        }        Collections.sort (bidlist, comparator);

Sort a collection in Java

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.