Two implementations of Java to sort list _java

Source: Internet
Author: User

Objective

The list interface in the Java.util package inherits the collection interface and is used to store objects, so when sorting these objects, either let the object class implement the comparison of the same objects themselves, or use the comparator to compare the sort.

Student entity class that contains the name and age attributes, sorted by name in ascending order, and sorted by age if the name is the same.

The first type: entity classes to implement their own comparison

(Implement comparable interface: public interface Comparable<T> , inside on a method declaration: public int compareTo(T o); )

Sample code:

public class Student implements comparable<student>{ 
 
 private String name; 
 private int age; 
 Public Student () { 
  super (); 
  TODO auto-generated constructor stub 
 } public 
 Student (String name, int age) { 
  super (); 
  this.name = name; 
  This.age = age; 
 } 
 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; 
 } 
 @Override public 
 int compareTo (Student o) { 
  //TODO auto-generated method stub 
  int flag = This.name.compar ETo (o.name); 
  if (flag = = 0) { 
   flag = this.age-o.age; 
  } 
  return flag; 
 }  

Then use the list class sort(Comparator<? super E> c) method or java.util.Collections tool class sort(List<T> list) (in fact, in a sentence: list.sort(null); ) to sort:

list<student> students = new arraylist<student> (); 
Students.add (New Student ("a",)); 
Students.add (New Student ("B",)); 
Students.add (New Student ("B", one)); 
Students.add (New Student ("AC"); 
Students.sort (NULL); 

Results:

  A of
  ac
  B
  12

Second: Sort by comparator.

Sample code:

 public class Student { 
 
 private String name; 
 private int age; 
 Public Student () { 
  super (); 
  TODO auto-generated constructor stub 
 } public 
 Student (String name, int age) { 
  super (); 
  this.name = name; 
  This.age = age; 
 } 
 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; 
 } 
  

The comparator java.util.Comparator class is an interface ( public interface Comparator<T> ), containing int compare(T o1, T o2); methods such as:

Our comparer implements the interface and implements the compare method:

Private class Studentcomparator implements comparator<student> { 
 
 @Override public 
 int compare (Student O1 , Student O2) { 
  //TODO auto-generated method stub 
  int flag = O1.getname (). CompareTo (O2.getname ()); 
  if (flag = = 0) { 
   flag = o1.getage ()-o2.getage (); 
  } 
  return flag; 
 } 
  

You can sort by using the list sort(Comparator<? super E> c) method (or java.util.Collections the method of the tool class) when you compare it sort(List<T> list, Comparator<? super T> c) .

list<student> students = new arraylist<student> (); 
Students.add (New Student ("a",)); 
Students.add (New Student ("B",)); 
Students.add (New Student ("B", one)); 
Students.add (New Student ("AC"); 
Test T = new test (); 
Students.sort (T.new studentcomparator ()); 
Collections.sort (Students, T.new studentcomparator ()); 
for (Student student:students) { 
 System.out.println (student.getname () + "" +student.getage ()); 

The result is the same as the first method:

  A of
  ac
  B
  12

Summarize

The above is about the list in Java all the contents of the order, I hope this article on the content of everyone's study or work can bring certain help, if you have questions you can message exchange.

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.