主要是利用jakarta的common提供的方面来进行排序。具体演示看代码
1、创建一个需要排序的对象
- public class SignMessage {
- private long id;
- private String name;
- private int count;
- public long getId() {
- return id;
- }
- public void setId(long id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getCount() {
- return count;
- }
- public void setCount(int count) {
- this.count = count;
- }
- }
创建我们的排序类,将分为对单个属性的升序降序排列。对多个属性的升序降序排列,对多个属性的升序降序排列将采用自我实现比较接口的方式和使用排序chain方式进行实现
- package com.kongqz.test;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.List;
- import org.apache.commons.beanutils.BeanComparator;
- import org.apache.commons.collections.comparators.ComparatorChain;
- import org.apache.commons.collections.comparators.ReverseComparator;
- /**
- * 针对javabean的某个属性或者多个属性做排序处理
- * @author kongqz
- * @date 2008-09-22
- *
- * */
- public class CompareTest {
- /**
- * 初始化一个需要排序的集合
- * */
- public List<SignMessage> initData(){
- List<SignMessage> tmp = new ArrayList();
- for(int i=100;i>0;i--){
- SignMessage sm = new SignMessage();
- sm.setId(i);
- if(i<50){
- sm.setCount(i+10000);
- }else{
- sm.setCount(i+9950);
- }
- sm.setName("kongqz"+i);
- tmp.add(sm);
- }
- for(SignMessage sm:tmp){
- System.out.println(sm.getName()+"-["+sm.getCount()+"]");
- }
- return tmp;
- }
- /**
- * 对对象的单个属性count进行排序
- * @author kongqz
- * */
- public List<SignMessage> orderSM(List<SignMessage> smList){
- //创建针对某个属性的升序比较
- Comparator countCompare = new BeanComparator("count");
- //默认的是升序,整一个降序
- countCompare=new ReverseComparator(countCompare);
- //开始排序
- Collections.sort(smList,countCompare);
- return smList;
- }
- /**
- * 直接实现排序的Comparator接口。重构其比较函数,让我们的业务逻辑直接写入
- * @author kongqz
- * */
- public List<SignMessage> orderMutiSM(List<SignMessage> smList){
- Comparator ccc =new Comparator() {
- public int compare(Object o1, Object o2) {
- SignMessage sm1 = (SignMessage) o1;
- SignMessage sm2 = (SignMessage) o2;
- //请在此处扩展你的计算逻辑
- int count1 = sm1.getCount();
- int count2 = sm2.getCount();
- Integer ct1 = new Integer(count1);
- Integer ct2 = new Integer(count2);
- if(count1!=count2){
- //升序降序通过ct2和ct1谁在前控制
- return ct2.compareTo(ct1);
- }else{
- // System.out.println("调用id比较了");
- Integer id1=new Integer(sm1.getId()+"");
- Integer id2=new Integer(sm2.getId()+"");
- //升序降序通过id1和id2谁在前控制
- return id1.compareTo(id2);
- }
- }
- };
- //将属性值为null的时候就是根据实现的Comparator来进行比较了,此处可以查看commons-beanutils源代码确认
- Comparator objCompare = new BeanComparator(null,ccc );
- //这个默认的是升序
- Collections.sort( smList, objCompare );
- return smList;
- }
- /**
- * order by count desc,id asc方式的排序
- * @author kongqz
- * 此排序调用已有的jakarta的接口实现
- * */
- public List<SignMessage> sortMutiSM(List<SignMessage> smList){
- //创建多属性排序链
- ComparatorChain cc = new ComparatorChain();
- //第一个参数是指定排序的属性,第二个参数指定降序升序
- cc.addComparator(new BeanComparator("count"),true);
- cc.addComparator(new BeanComparator("id"),false);
- Collections.sort( smList, cc );
- return smList;
- }
- public static void main(String[] args){
- CompareTest ct = new CompareTest();
- System.out.println("----------------单个javabean属性的排序-------------------------");
- List<SignMessage> singleList = ct.initData();
- List<SignMessage> singleTmpList=ct.orderSM(singleList);
- System.out.println("-----------------javabean的单个属性排序,排序后-------------------");
- for(SignMessage sm:singleTmpList){
- System.out.println(sm.getName()+" ["+sm.getCount()+"]");
- }
- System.out.println("----------------重载接口进行多属性的排序-------------------------");
- //初始化一个需要排序的集合
- List<SignMessage> smList = ct.initData();
- List<SignMessage> tmpList = ct.orderMutiSM(smList);
- System.out.println("-------------------------直接实现多属性的排序,排序后------------");
- for(SignMessage sm:tmpList){
- System.out.println(sm.getName()+" ["+sm.getCount()+"]");
- }
- System.out.println("----------------使用jakarta方法进行排序-------------------------");
- //初始化一个需要排序的集合
- List<SignMessage> sList = ct.initData();
- //进行排序
- List<SignMessage> tmpList1 = ct.sortMutiSM(sList);
- System.out.println("-------------------------使用jakarta提供的排序chain进行排序,排序后------------");
- for(SignMessage sm:tmpList1){
- System.out.println(sm.getName()+" ["+sm.getCount()+"]");
- }
- }
- }
运行main函数,控制台看具体执行效果