How to sort list elements in Java _java

Source: Internet
Author: User
Tags comparable

The list implementation defined in the Java Collection Framework has vector,arraylist and LinkedList. These collections provide index access to the group of objects. They provide support for adding and removing elements. However, they do not have built-in element ordering support.
You can sort the list element by using the sort () method in the Java.util.Collections class. You can either pass a list object to the method or pass a list and a comparator. If the elements in the list are all classes of the same type, and the class implements the comparable interface, you can simply invoke Collections.sort (). If this class does not implement comparator, you can also pass a comparator to the method sort () and sort. If you do not want to use the default sorting order, you can also pass a comparator to the method sort () to sort.

1. The object of comparison implements comparable interface

 public class Student implements comparable {private int id; 
 private int age; 
 private String name; 
 public Student (int id) {this.id=id; 
 public int getId () {return id; 
 The public void setId (int id) {this.id = ID; 
 public int getage () {return age; 
 public void Setage (int age) {this.age = age; 
 Public String GetName () {return name; 
 public void SetName (String name) {this.name = name; 
  public static void Main (String args[]) {list<student> list=new arraylist<student> (); 
  for (int i=1000;i>0;i--) {List.add (new Student (i)); 
  } collections.sort (list); 
  for (Student s:list) {System.out.println (S.getid ()); 
   } public int compareTo (Object o) {if (o instanceof Student) {Student s= (Student) o; 
   if (this.id>s.id) {return 1; 
   } else{return 0; 
 }} return-1; } 
 
} 

2. Use Collections.sort (Object o,comparator c)

public class Jiamenucomparator implements comparator{public 
 
 int Compare (object O1, Object O2) { 
  if (null!=o1 &&null!=o2) 
  { 
   jiamenu menu1= (jiamenu) O1; 
   Jiamenu menu2= (Jiamenu) O2; 
   if (Menu1.getid () <menu2.getid ()) {return 
    1; 
   } else {return 
    0; 
   } 
  } 
  return 0; 
 } 
  
} 

3. Sort multiple attributes in a list element (using Commons-beanutils)
The Commons-beanutils class Library has a Beancomparator class that can be used to sort JavaBean. But this class can only sort on one property at a time. Here is an example of using Beancomparator to sort multiple properties of JavaBean, very simple:

Import Org.apache.commons.beanutils.BeanComparator;
Import java.util.Collections;
Import Java.util.Comparator;
Import java.util.List;
 
/**
 * Class for multiple properties of the List element
/@SuppressWarnings ({"Unchecked"}) public
class Listsorter {
 
 /**
  * Multiple properties of the List element are sorted. For example, Listsorter.sort (list, "name", "age") is sorted by the
  * Name property, and the element with the same name is sorted by the age attribute.
  *
  @param list  contains the
  properties to be sorted by the list * @param properties to sort elements. The preceding values have high precedence.
  *
 /public static <V> void sort (list<v> List, Final String ... properties) {
  Collections.sort  (List, new comparator<v> () {public
   int compare (v O1, v O2) {
    if (O1 = null && O2 = null) return 0;
    if (O1 = = null) return-1;
    if (O2 = null) return 1;
 
    for (String property:properties) {
     Comparator c = new Beancomparator (property);
     int result = C.compare (O1, O2);
     If (Result!= 0) {return result
     }
    }
    return 0;}}
  );
 }

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.