The Java Collections.sort method sorts the list collection

Source: Internet
Author: User

1. Sequencing Test class

Package Com.ljq.test;import Java.util.arraylist;import Java.util.collections;import java.util.comparator;import Java.util.list;public class UserSort {public    static void Main (string[] args) {        list<user> userlist =new Ar Raylist<user> ();        Userlist.add (New User (1,2.2));        Userlist.add (New User (2,1.1));        Userlist.add (New User (3,4.4));        Userlist.add (New User (4,5.5));        Userlist.add (New User (5,3.3));                Collections.sort (userlist, New comparator<user> () {public            int compare (user u1, user u2) {                return new DOUBL E (U1.getsalary ()). CompareTo (New Double (U2.getsalary ())); Ascending                //return new double (U2.getsalary ()). CompareTo (New double (U2.getsalary ()));//Descending            }        });                for (user user:userlist) {            System.out.println (user);}}    }

2. User object, Store JavaBean property

Package Com.ljq.test;import java.io.serializable;/** * User * * @author Jqlin */public class user implements Serializable{
   private static final Long serialversionuid = 1L;        Private long ID; Primary key    private double salary;//Payroll Public    User () {    } public    user (long ID, double salary) {        super (); 
   this.id = ID;        This.salary = salary;    }    Public long GetId () {        return ID;    }    public void SetId (long id) {        this.id = ID;    }    Public double getsalary () {        return salary;    }    public void Setsalary (double salary) {        this.salary = salary;    }    @Override public    String toString () {        return "User [id=" + ID + ", salary=" + salary + "]";    }}

The Java Collections.sort method sorts the list 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.