Ordering members in a Java paradigm collection

Source: Internet
Author: User

The class in the generic collection is Jsonobject, not a custom class, and if it is a custom class, take the value of the field you want to compare directly.

1Arraylist<jsonobject> TList =NewArraylist<jsonobject>();2  3                 for(inti=0;i<10000;i++)4                 {5Jsonobject object=NewJsonobject ();6Random Rand =NewRandom ();7                     intRandnum = Rand.nextint (2000000) +5;8String value=string.valueof (randnum);9                     Try {TenObject.put ("Value", Value); OneObject.put ("name", "Liaohang"); A}Catch(jsonexception e) { -                         //TODO auto-generated Catch block - e.printstacktrace (); the                     } - Tlist.add (object); -                      -                       +                      -                 } +                A                at                -Collections.sort (TList,NewComparator<jsonobject>() { -                      //can change ' Name ' with ' ID ' if you want to sort by ID -                      Private Static FinalString key_name = "value"; -  - @Override in                       Public intCompare (Jsonobject A, jsonobject b) { -                          intValA =0; to                          intValb =0; +  -                          Try { theValA =integer.parseint (String) a.get (key_name)); *Valb =integer.parseint (String) b.get (key_name)); $                          } Panax Notoginseng                          Catch(jsonexception e) { -                              //Do something the                          } +                         returnValA > Valb? 1:-1; A                           the                        //return Vala.compareto (VALB); +                          //If you want to change the sort order, simply use the following: -                          //Return-vala.compareto (VALB); $                      } $                  }); -                 -                the                //print a sorted result -String printvalue=NULL;Wuyi                  for(intI=0;i<tlist.size (); i++) the                 { -Jsonobject obect=Tlist.get (i); Wu                   Try { -Printvalue=obect.getstring ("Value"); About}Catch(jsonexception e) { $                     //TODO auto-generated Catch block - e.printstacktrace (); -                 } -                         A System.out.println (printvalue); +                    the}

Ordering members in a Java paradigm collection

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.