Examples of comparator in Java

Source: Internet
Author: User

Business requirements:

Descending by number of participants, then by rank and ascending

Number of people: pertotal, ranking of the sum: ranktotal

Full code:

@Overridepublic jsonobject gettopunitofgameranking (list<map<long, integer>> usermap,int limit) { Jsonobject json = new Jsonobject (); list<jsonobject> list = new arraylist<jsonobject> (); list<jsonobject> lists = new arraylist<jsonobject> (); list<long> userids = new arraylist<long> (); for (Map<long, integer> Userranking:usermap) {for (Long UserId:userRanking.keySet ()) {Userids.add (userId);}} if (userids! = null && userids.size () > 0) {list<object> param = new arraylist<object> (); StringBuilder builder = new StringBuilder (); Builder.append ("Select Unit_id,count (user_id) as Pertotal from T_user_ Expand where unit_id!=0 and user_id in ("), for (int i = 0; I < userids.size (); i++) {if (I! = 0) {builder.append (",");} Builder.append ("?"); Param.add (Userids.get (i)); Builder.append (") GROUP by unit_id ORDER BY pertotal desc limit?"); Param.add (limit);//final list<long> unitids = new arraylist<long> (); List = prImaryjdbctemplate.query (Builder.tostring (), Param.toarray (), new rowmapper<jsonobject> () {@Overridepublic Jsonobject Maprow (ResultSet rs, int rowNum) throws SQLException {jsonobject o = new Jsonobject (); O.put ("Unitid", Rs.getlon G ("unit_id")); O.put ("Pertotal", Rs.getlong ("Pertotal")); return o;}); list<object> params = new arraylist<object> (); StringBuilder builders = new StringBuilder (); Builders.append ("Select unit_id,user_id from T_user_expand where unit_id! = 0 and user_id in ("), for (int i = 0; I < userids.size (); i++) {if (I! = 0) {builders.append (",");} Builders.append ("?"); Params.add (Userids.get (i)); Builders.append (") limit?"); Params.add (limit); lists = Primaryjdbctemplate.query (Builders.tostring (), Params.toarray (), New rowmapper< Jsonobject> () {@Overridepublic jsonobject Maprow (ResultSet rs, int rowNum) throws SQLException {jsonobject o = new JSON Object (); O.put ("Unitid", Rs.getlong ("unit_id")), O.put ("user_id", Rs.getlong ("user_id")); return o;}); for (JSONobject obj:lists) {//traverse "User ID" associated "Province ID" for (map<long, integer> user:usermap) {///by "User ID" to get the corresponding ranking Integer rank =user . Get (Obj.get ("user_id")); if (null = = rank) {rank = 0;} for (Jsonobject object:list) {if ((long) object.get ("Unitid"). CompareTo ((Long) obj.get ("Unitid")) {Integer Oldranktotal = (Integer) object.get ("Ranktotal"); if (null = = Oldranktotal) {oldranktotal = 0;} Object.put ("Ranktotal", Oldranktotal + Rank);}}}} Collections.sort (list, new comparator<jsonobject> () {/** *) first descending by number of participants and then ascending by rank */@Overridepublic int Compare ( Jsonobject O1, Jsonobject O2) {Long perTotal1 = (long) o1.get ("Pertotal"); Long PerTotal2 = (long) o2.get ("Pertotal"), Integer rankTotal1 = (integer) o1.get ("Ranktotal"), Integer rankTotal2 = ( Integer) o2.get ("Ranktotal"); if (Pertotal1.compareto (PerTotal2) > 0) {return-1;} else if (Pertotal1.compareto (PerTotal2) < 0) {return 1;} Else{if (Ranktotal1.compareto (RankTotal2) > 0) {return 1;} else if (Ranktotal1.compareto (RankTotal2) < 0) {return-1;} Else{return 0;}}});Json.put ("Data", list); return JSON;} 

key part code (in the same number of cases, compare the sum of the rankings):

list<jsonobject> list = new arraylist<jsonobject> (); Collections.sort (list, new comparator<jsonobject> () {/** *) first descending by number of participants and then ascending by rank */@Overridepublic int Compare ( Jsonobject O1, Jsonobject O2) {Long perTotal1 = (long) o1.get ("Pertotal"); Long PerTotal2 = (long) o2.get ("Pertotal"), Integer rankTotal1 = (integer) o1.get ("Ranktotal"), Integer rankTotal2 = ( Integer) o2.get ("Ranktotal"); if (Pertotal1.compareto (PerTotal2) > 0) {return-1;} else if (Pertotal1.compareto (PerTotal2) < 0) {return 1;} Else{if (Ranktotal1.compareto (RankTotal2) > 0) {return 1;} else if (Ranktotal1.compareto (RankTotal2) < 0) {return-1;} Else{return 0;}}});




Examples of comparator 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.