Home>

The list implementations defined in the java collection framework are vector, arraylist and linkedlist. These collections provide indexed access to groups of objects.They provide support for adding and removing elements.However, they do not have built-in element ordering support.

You can use the sort () method in the java.util.collections class to sort the list elements.You can pass either a list object to the method, or a list and a comparator. If the elements in the list are all classes of the same type,And this class implements the comparable interface, you can simply call collections.sort (). If this class does not implement a comparator, you can also pass a comparator to the sort () method for sorting.If you don't want to use the default sort order for sorting,You can also pass a comparator to the sort () method to sort.

1. The compared objects implement the 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;
 }
 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 the list element (using commons-beanutils)

The commons-beanutils class library has a beancomparator class that can be used to sort javabeans. But this class can only sort one attribute at a time.Here is an example of using beancomparator to sort multiple properties of a javabean,very simple:

import org.apache.commons.beanutils.beancomparator;
import java.util.collections;
import java.util.comparator;
import java.util.list;
/**
 * Class to sort multiple attributes of a list element
 * /
@suppresswarnings ({"unchecked"})
public class listsorter {
 /**
  * Sort multiple elements of a list element.
For example listsorter.sort (list, "name", "age"), press
  * name attribute sorting,Elements with the same name are sorted by the age attribute.
  *
  * @param list list containing elements to be sorted
  * @param properties The properties to be sorted.
The previous value takes 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;
   }
  });
 }
}
  • Previous Summary of Database Development for JSP Learning
  • Next struts2 Demo Demo