Sorting objects in the list in Java implementation

Source: Internet
Author: User

 Packagecom.test;Importjava.util.ArrayList;Importjava.util.Collections;ImportJava.util.Comparator;Importjava.util.List; Public classNewsmanager {/**     * @paramargs*/     Public Static voidMain (string[] args) {List Newss=getnewslist ();  for(intI=0;i<newss.size (); i++) {News News=(News) newss.get (i); System.out.println ("ID:" +News.getid ()); System.out.println ("Title:" +news.gettitle ()); System.out.println ("Hits:" +news.gethits ()); }    }             Public StaticList getnewslist () {List List=NewArrayList (); News news1=NewNews (); News1.sethits (1); News1.setid (1); News1.settitle ("Test1");                List.add (NEWS1); News news2=NewNews (); News2.sethits (7); News2.setid (2); News2.settitle ("Test2");                List.add (NEWS2); News NEWS3=NewNews (); News3.sethits (3); News3.setid (3); News3.settitle ("Test3");                List.add (NEWS3); News News4=NewNews (); News4.sethits (5); News4.setid (4); News4.settitle ("Test4");                        List.add (NEWS4); //reverse by number of clicksCollections.sort (list,NewComparator<news>() {             Public intCompare (news arg0, news arg1) {intHits0 =arg0.gethits (); intHits1 =arg1.gethits (); if(Hits1 >hits0) {                    return1; } Else if(Hits1 = =hits0) {                    return0; } Else {                    return-1;                }            }        }); returnlist; }}

Sorting objects in the list in Java implementation

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.