Java is sorted in descending order by field of the date format of the object

Source: Internet
Author: User
Tags java util

Java is sorted in descending order by field of the date format of the object

The method first applied to the Comparator<t> interface in the Java Util tool class:

The Compare (object O1,object O2) method is a method of the Java.util.comparator<t> interface, which actually uses the CompareTo (object o) method of the object to be compared.


Concrete Examples:

User object:

Import java.io.Serializable;

Import Java.util.Date; /** * @author wangmuming * */public class User implements Comparable<object> Serializable {/** * * * p

	Rivate static final Long serialversionuid = 1L;

	private int id;

	private String name;

	Private Date birthday;
		public User (int ID, String name, Date birthday) {this.id = ID;
		THIS.name = name;
	This.birthday = Birthday;
	* * * getters and setters/public int getId () {return id;
	The public void setId (int id) {this.id = ID;
	Public String GetName () {return name;
	public void SetName (String name) {this.name = name;
	Public Date Getbirthday () {return birthday;
	The public void Setbirthday (Date birthday) {this.birthday = birthday;
		@Override public int compareTo (Object o) {if (this = O) {return 0;
			else if (o!= null && o instanceof user) {User U = (user) O; if (U.getbirthday (). GetTime () < This.birthday.getTime ())//sorted by date Return-1;
			else return 1;
			if (ID <= u.id) {//sorted by ID//return-1;
			else {//return 1;
		} else {return-1;  @Override public String toString () {return "user is:id:" +this.id + "| Name: "+this.name +" |
	Birthday: "+this.birthday.tolocalestring (); }


Test method:

Import java.util.ArrayList;
Import java.util.Collections;
Import Java.util.Comparator;

Import Java.util.Date; public class test{//write Comparator, sort user According to user birthday private static final comparator<user> Comparator = new Comparator<user> () {public int compare (user O1, user O2) {return O1.compareto (O2);//Use User
    
    The CompareTo method of the class compares two objects}};
        public static void Main (string[] args) {arraylist<user> User = new arraylist<user> ();
        User User1 = new User (1, "wangmm1", New Date ());
        User User3 = New User (2, "wangmm2", new Date (new Date (). GetTime () +20000));
        User User2 = New User (3, "wangmm3", new Date (new Date (). GetTime () +10000));
        User User4 = New User (4, "wangmm3", new Date (new Date (). GetTime () +50000));
        User.add (User2);
        User.add (user1);
        User.add (USER3);
      
    
        User.add (USER4);
       Collections.sort (user, COMPARATOR)//////////////COMPARATOR for (int i=0;i<user.size (); i++) {System.out.println (User.get (i). toString ()); }
    }


Execution results:

User is:id:4| name:wangmm3| Birthday:2014-3-10 17:34:58
User is:id:2| name:wangmm2| Birthday:2014-3-10 17:34:28
User is:id:3| name:wangmm3| Birthday:2014-3-10 17:34:18
User is:id:1| name:wangmm1| Birthday:2014-3-10 17:34:08


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.