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