Arraylist object sorting program in java

Source: Internet
Author: User
The code is as follows: Copy code

 

Import java. util. ArrayList;
Import java. util. Collection;
Import java. util. Collections;
Import java. util. Comparator;
  
  
Public class SortTwo implements Comparator {
// Sort fields
Private String sort1;
// Sorting method
Private int state;
Public SortTwo (int state ){
This. state = state;
}   
Public SortTwo (){
  
}   
Public int SortUp (Object o1, Object o2 ){
Student stu = (Student) o1;
Student stu1 = (Student) o2;
If (sort1.equals ("name ")){
If (stu. getName (). compareTo (stu1.getName ()> 1 ){
Return 1;
} Else if (stu. getName (). compareTo (stu1.getName () <1 ){
Return-1;
} Else {
Return 0;
}   
}   
If (sort1.equals ("age ")){
If (stu. getAge (). compareTo (stu1.getAge ()> 1 ){
Return 1;
} Else if (stu. getAge (). compareTo (stu1.getAge () <1 ){
Return-1;
} Else {
Return 0;
}   
}   
Return 0;
}   
Public int SortDOWN (Object o1, Object o2 ){
Student stu = (Student) o1;
Student stu1 = (Student) o2;
If (sort1.equals ("name ")){
If (stu. getName (). compareTo (stu1.getName ()> 1 ){
Return-1;
} Else if (stu. getName (). compareTo (stu1.getName () <1 ){
Return 1;
} Else {
Return 0;
}   
}   
If (sort1.equals ("age ")){
If (stu. getAge (). compareTo (stu1.getAge ()> 1 ){
Return-1;
} Else if (stu. getAge (). compareTo (stu1.getAge () <1 ){
Return 1;
} Else {
Return 0;
}   
}   
Return 0;
}   
Public int compare (Object o1, Object o2 ){
  
If (this. state = 1 ){
Return SortUp (o1, o2 );
} Else {
Return SortDOWN (o1, o2 );
}   
}   
  
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
Student stu = new Student ();
Stu. setName ("123 ");
Stu. setAge ("12 ");
Student stu1 = new Student ();
Stu1.setName ("234 ");
Stu1.setAge ("23 ");
ArrayList <Student> list = new ArrayList <Student> ();
List. add (stu );
List. add (stu1 );
SortTwo t = new SortTwo (1 );
T. sort1 = "age ";
Collections. sort (list, t );
For (Student st: list ){
System. out. println (st. getName () + "" + st. getAge ());
}   
}   
  
}   

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.