Commons-beanutils provides a number of features, one of which is useful for sorting object collections, such as Collections.sort (peoples, New Beancomparator ("Age"));
In addition, you can use the Java.util.Collections sort method to sort the collection collection, including multi-column combination sorting, which is an example of customizing the object property Collation yourself Java.util.Comparator:
Package com.lph.test;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.LinkedList;
/**
*
* Custom Object Multi-attribute (multi-column) combination sorting
* <p>title:is a class</p>
*
* <p>description: Class </p>
*
* <p>copyright:copyright (c) 2006</p>
*
* <p>Company:sunrise</p>
*
* @author Islph
* @version 1.0
*/
Public class Compositecomparator implements Comparator {
/** in the Condition list, comparators ' priority decrease from head to tail */
Private List comparators = new linkedlist ();
/** get the comparators, you can manipulate it as need. */
Public List getcomparators () {
return comparators;
}
/** add a batch of comparators to the condition list */
Public void addcomparators (comparator[] comparatorarray) {
if (Comparatorarray = = null) {
return;
}
for (int i = 0; i < comparatorarray.length; i++) {
Comparators.add (Comparatorarray[i]);
}
}
/** compare by the priority */
Public int Compare (object O1, Object O2) {
for (Iterator Iterator = Comparators.iterator (); Iterator.hasnext ();) {
Comparator Comparator = (Comparator) iterator.next ();
int result = Comparator.compare (O1, O2);
if (Result! = 0) {
return result;
}
}
return 0;
}
}
Sort by attribute County_no town, ddp_id terminal combination
Compositecomparator mycomparator = new compositecomparator ();
Mycomparator.addcomparators (
New Comparator[] {
New Comparator () {
Public int Compare (object A, object B) {
DisplayForm Aobj, Bobj;
Aobj = (displayform) A;
Bobj = (displayform) b;
String aStr = (string) aobj.get ("County_no");
String bStr = (string) bobj.get ("County_no");
return Astr.compareto (BSTR);
}
}
, new Comparator () {
Public int Compare (object A, object B) {
DisplayForm Aobj, Bobj;
Aobj = (displayform) A;
Bobj = (displayform) b;
String aStr = (string) aobj.get ("ddp_id");
String bStr = (string) bobj.get ("ddp_id");
return Astr.compareto (BSTR);
}
}
});
Java.util.Collections.sort (Listresult, mycomparator);
Implement the Java.util.Comparator interface and sort the collection of objects in multiple combinations of attributes