zoukankan      html  css  js  c++  java
  • 实现java.util.Comparator接口,对对象集合进行多属性组合排序

    Commons - BeanUtils 提供了很多功能,其中一个很有用的是对对象集合进行排序,如Collections.sort(peoples, new BeanComparator("age"));

    另外,可以使用java.util.Collections的sort方法可以对collection集合进行排序,包括多列组合排序,下面是自己实现java.util.Comparator,定制对象属性排序规则的例子:

    package com.lph.test;

    import java.util.Comparator;

    import java.util.Iterator;

    import java.util.List;

    import java.util.LinkedList;

    /** 

    * 自定义对象多属性(多列)组合排序
    * <p>Title: is a Class</p> 

    * <p>Description: 类</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;

        }

    }

     

    // 多列排序;按属性COUNTY_NO镇,DDP_ID终端 组合排序

    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);

  • 相关阅读:
    linux service 例子
    YII2自动初始化脚本
    ubuntu 如何在命令行打开当前目录
    mysql 储存过程
    Mysql 随笔记录
    Lack of free swap space on Zabbix server
    意外发现PHP另一个显示转换类型 binary
    常用的排序代码
    线程的实现方式之内核支持线程和用户级线程
    寻找二叉树中的最低公共祖先结点----LCA(Lowest Common Ancestor )问题(递归)
  • 原文地址:https://www.cnblogs.com/littleCode/p/4228793.html
Copyright © 2011-2022 走看看