ArrayList container sorting Comparator interface and comparable interface use __java

Source: Internet
Author: User

1, the entity class implements the comparable interface, the rewriting CompareTo method

Package io;
Import Java.text.DateFormat;
Import java.text.ParseException;
Import Java.text.SimpleDateFormat;

Import Java.util.Date;
	public class Student implements comparable<student>{private String name;
	private int age;
	
	
	
	Private Date birthday;
		Public Student (string name, int age, string birthday) {super ();
		THIS.name = name;
		
		This.age = age;
		DateFormat ds=new SimpleDateFormat ("Yyyy-mm-dd");
		try {this.birthday = Ds.parse (birthday);
		catch (ParseException e) {e.printstacktrace ();
	};
	Public String GetName () {return name;
	public void SetName (String name) {this.name = name;
	public int getage () {return age;
	public void Setage (int age) {this.age = age;
	Public Date Getbirthday () {return birthday;
	The public void Setbirthday (Date birthday) {this.birthday = birthday;
		///Descending by name in ascending birthday @Override public int compareTo (Student o) {int result=0;
		Result=-this.name.compareto (O.name); if (result==0) {RESult=this.birthday.compareto (O.birthday);
	return result; @Override public String toString () {return "Student [name=] + name +", age= + age +, birthday= "+ Birthday +
	"]";
 }
	

}

Package io;

Import java.util.ArrayList;
Import java.util.Collections;
Import java.util.List;

public class App. {public

	static void Main (string[] args) {
		
		list<student> list=new arraylist<student > ();
		List.add (New Student ("John", "1987-10-5"));
		List.add (New Student ("Harry", "1986-10-5"));
		List.add (New Student ("Zhao Liu", "1988-10-5"));
		List.add (New Student ("Wang Qi", "1980-10-5"));
		Collections.sort (list);
		for (Student s:list) {
			System.out.println (s);
		}
	}
}



2, create a new business class implementation comparator interface, rewrite the Compare method.


package io;
Import Java.text.DateFormat;
Import java.text.ParseException;
Import Java.text.SimpleDateFormat;

Import Java.util.Date;
	public class Student {private String name;
	private int age;
	
	
	
	Private Date birthday;
		Public Student (string name, int age, string birthday) {super ();
		THIS.name = name;
		
		This.age = age;
		DateFormat ds=new SimpleDateFormat ("Yyyy-mm-dd");
		try {this.birthday = Ds.parse (birthday);
		catch (ParseException e) {e.printstacktrace ();
	};
	Public String GetName () {return name;
	public void SetName (String name) {this.name = name;
	public int getage () {return age;
	public void Setage (int age) {this.age = age;
	Public Date Getbirthday () {return birthday;
	The public void Setbirthday (Date birthday) {this.birthday = birthday; @Override public String toString () {return "Student [name=] + name +", age= + age +, birthday= "+ Birthday
	+ "]"; }
	

}
Package io;

Import Java.util.Comparator;

The public class Birthdaycomp implements comparator<student>{


	@Override
	the public int compare (Student O1, Student O2) {return
	
		o1.getbirthday (). CompareTo (O2.getbirthday ());
	}

}

Package io;

Import java.util.ArrayList;
Import java.util.Collections;
Import java.util.List;

public class App. {public

	static void Main (string[] args) {
		
		list<student> list=new arraylist<student& gt; ();
		List.add (New Student ("John", "1987-10-5"));
		List.add (New Student ("Harry", "1986-10-5"));
		List.add (New Student ("Zhao Liu", "1988-10-5"));
		List.add (New Student ("Wang Qi", "1980-10-5"));
		Collections.sort (list, new Birthdaycomp ());
		for (Student s:list) {
			System.out.println (s);
		}
	}
}










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.