实现对List quoCCList 进行排序

package test;


import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 功能简述:
 * QuoCostCenter这个对象中包含basCostCenterId这个属性;
 * 以下代码实现对List<QuoCostCenter> quoCCList 进行排序,
 * 要求按  QuoCostCenter 中的 basCostCenterId属性进行降序排列
 *
 * 关键:创建排序器(ComparatorType类)
 *
 */
public class TestComparator
{
    //排序类-basCostCenterId-Long
        private ComparatorType upComparator;
   
        QuoCostCenter quoCC = new QuoCostCenter();
        List<QuoCostCenter> quoCCList= new ArrayList<QuoCostCenter>();
     // 对明细列表按QuoCostCenter中的成本中心ID升序排
        upComparator = new ComparatorType();
        Collections.sort(newQuoCostCenters, upComparator);
        return newQuoCostCenters;
   
// 设置比较器,按QuoCostCenter中的BasCostCenterId(Long)比较
private class ComparatorType implements Comparator<Object>
{
   
    public int compare(Object arg0, Object arg1)
    {
        QuotaCostCenter obj1 = (QuotaCostCenter) arg0;
        QuotaCostCenter obj2 = (QuotaCostCenter) arg1;
        // 改变obj2 和 obj1 的先后顺序可以控制排序顺序
        return obj1.getBasCostCenterId().compareTo(obj2.getBasCostCenterId());
    }
   
}
}

 

你可能感兴趣的:(java,apache)