Reorder the list

Source: Internet
Author: User

Like I have a javabean://department class public class territory{   private int id;    private string territoryname;   public void setid (Int id) {        this.id = id;   }   public  Int getid () {       return id;   }    public void setterritoryname (String territoryname) {        this.territoryname = territoryname;   }   public string  getterritoryname () {       return territoryname;    }} a Business class:public class datamigrationdao{    territory t1 =  New territory ();     t1.setid (5);     t1.setterritoryname ("T1");      terriTory t2 = new territory ();      t2.setid (1);      t2.setterritoryname ("T2");     territory t3 = new  Territory ();      t2.setid (3);      t2.setterritoryname ("T3");        List<Territory> list = new ArrayList< Territory> ();       list.add (t1);       list.add ( T2);       list.add (T3);       //now sort the list collection in ascending order       collections.sort (List, new compareunit ());       .....      //will reorder the list to persist other operations such as}//inner class//This is ascending, if you want to descending, put the if (.. (...) {return -1} if (.. &lt, ...) {Return 1} return 0class compareunit implements comparator<territory>{     @Override     public int compare (Territory t1 ,  territory t2)  {        if (T1.getId () <t2.getId ()) {             return -1;         }        if (T1.getId () >t2.getId ()) {             return 1;         }           return 0;     }   }

Reordering the list

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.