Implement the Java.util.Comparator interface and sort the collection of objects in multiple combinations of attributes

Source: Internet
Author: User

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

Related Article

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.