Sorting using methods in Java __java

Source: Internet
Author: User
Package model;
Import Java.text.SimpleDateFormat;

Import Java.util.Date;
Import Org.apache.commons.lang.StringUtils;


Import Org.apache.commons.lang.builder.ToStringBuilder;

    public class Person {private String name;

    private int age;
    
    private double salary;

    Private Date birthday;
     Public person () {}/** * @param name * @param age * @param salary * @param birthday
        */Public person (String name, int age, double salary, Date birthday) {this.name = name;
        This.age = age;
        This.salary = salary;
    This.birthday = Birthday; /** * @param name * @param age * @param salary/Public person (String name, int age, Doub
        Le salary) {this.name = name;
        This.age = age;
    This.salary = salary; /** * @param name * @param age */Public person (String name, int age) {this.name = name
        ; This.age = age;
    The/** * Getter method is for the property <tt>name</tt>.
    * @return Property value of Name * */public String GetName () {return name;
     /** * Setter method to property <tt>name</tt>. * @param name value to is assigned to the property name */public void SetName (String name) {this.na
    me = name;
     The/** * Getter method is for the property <tt>age</tt>.
    * * @return Property value of age */public int getage () {return age;
     /** * Setter method to property <tt>age</tt>. * @param age value to is assigned to the property age/public void setage (int age) {This.age = age
    ;
     The/** * Getter method is for the property <tt>salary</tt>.
    * @return Property value of salary */public double getsalary () {return salary; }/** * Setter method For property <tt>salary</tt>.
        * * @param salary value to is assigned to the property salary/public void setsalary (double salary) {
    This.salary = salary;
     The/** * Getter method is for the property <tt>birthday</tt>.
    * @return Property value of Birthday */public Date Getbirthday () {return birthday;
     /** * Setter method to property <tt>birthday</tt>.
        * * @param birthday value to is assigned to the property birthday/public void Setbirthday (Date birthday) {
    This.birthday = Birthday; /** * @see java.lang.object#tostring () * * * @Override public String toString () {Simpleda
        Teformat format=new SimpleDateFormat ("Yyyy-mm-dd");
        String finalbirthday= "";
        if (This.getbirthday ()!=null) {Finalbirthday=format.format (birthday); Return ' person [name= ' + name + ', age= ' + age + ", salary=" + salary + ", birthday=" + Finalbirthday + "]";
 }

  
}
Package sort;
Import Java.util.Comparator;
Import Java.util.Date;
Import java.util.List;

Import java.util.stream.Collectors;

Import com.google.common.collect.Lists; Import model.

person; public class Personsort {private void Initperson (list<person> persons) {person p = new person ("whangs
        An ", 12.5, New Date (2017, 11, 12));
        person P1 = new Person ("Zhangsan", 12.5, New Date (2017, 11, 13));
        person P2 = new Person ("Zhangsan", 13.5, new Date (2017, 10, 12));
        Person P3 = new Person ("Zhangsan", 13.5, new Date (2017, 10, 9));
        person P4 = new Person ("Zhangsan", one, 10.5, new Date (2017, 12, 11));
        Person P5 = new person ("Zhangsan", 11, 10.5);
        Persons.add (P);
        Persons.add (p1);
        Persons.add (p2);
        Persons.add (p3);
        Persons.add (p4);
    Persons.add (p5); } private void Print (list<person> persons) {for (person p:persons) {System.out.println (
   p);     The public static comparator<person> sort () {comparator<person> sortbyname = (O1, O2)
        -> o1.getname (). CompareTo (O2.getname ()));
        Comparator<person> sortbyage = (O1, O2)-> Integer.compare (O1.getage (), O2.getage ());
        Comparator<person> sortbysalary = (O1, O2)-> Double.compare (O1.getsalary (), o2.getsalary ());
        Comparator<person> sortbydate = (O1, O2)-> o1.getbirthday (). CompareTo (O2.getbirthday ())); Comparator<person> Sortbybirthday = (O1, O2)-> Comparator.nullsfirst (comparator.<date> rever
        Seorder ()). Compare (O1.getbirthday (), O2.getbirthday ()); Return sortbyname.thencomparing (sortbyage.reversed ()). Thencomparing (Sortbysalary). thencomparing (SortByBirthda

    y);  }/** * * @param args/public static void main (string[] args) {Personsort Personsort =
        New Personsort (); List<person>data = Lists.newarraylist ();
        list<person> NewData = Lists.newarraylist ();
        Personsort.initperson (data);
        Personsort.print (data);
        SYSTEM.OUT.PRINTLN ("------");
        NewData = Data.stream (). Sorted (sort ()). Collect (Collectors.tolist ());
        Personsort.print (NewData);
        SYSTEM.OUT.PRINTLN ("------");

    Personsort.print (data);
 }

}


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.