Sorting objects in the list in Java implementation

Source: Internet
Author: User

Package Com.test;import Java.util.arraylist;import Java.util.collections;import java.util.comparator;import Java.util.list;public class Newsmanager {/** * @param args */public static void main (string[] args) {List Newss=getnewslis T (); for (int i=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 static list getnewslist () {list list=new ArrayList (); News News1=new News (); news1.sethits (1); News1.setid (1); News1.settitle ("test1"); List.add (NEWS1); News News2=new News (); news2.sethits (7); News2.setid (2); News2.settitle ("test2"); List.add (NEWS2); News News3=new News (); news3.sethits (3); News3.setid (3); News3.settitle ("test3"); List.add (NEWS3); News News4=new News (); news4.sethits (5); News4.setid (4); News4.settitle ("test4"); List.add (NEWS4);// By number of hits reverse Collections.sort (list, new comparator<news> () {public int compare (news arg0, news arg1) {int hits0 = Arg0.geth its (); Int hitS1 = Arg1.gethits (), if (Hits1 > Hits0) {return 1;} else if (hits1 = = hits0) {return 0;} else {return-1;}}); return list;}}

  

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.