java如何对list进行排序

package com.fh.util;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import org.apache.commons.beanutils.BeanComparator;
import org.apache.commons.collections.ComparatorUtils;
import org.apache.commons.collections.comparators.ComparableComparator;
import org.apache.commons.collections.comparators.ComparatorChain;
 
public class SortUtil {
    public static void main(String[] args) {
        System.out.println(testMapSort());
    }
     
    /**
     * 对list进行排序
     * @param sortList 需要排序的list
     * @param param1   排序的参数名称
     * @param orderType 排序类型:正序-asc;倒序-desc  
     */
    public static List sort(List sortList, String param1, String orderType){
        Comparator mycmp1 = ComparableComparator.getInstance ();
        if("desc".equals(orderType)){
            mycmp1 = ComparatorUtils. reversedComparator(mycmp1); //逆序(默认为正序)
        }
         
        ArrayList<Object> sortFields = new ArrayList<Object>();
        sortFields.add( new BeanComparator(param1 , mycmp1)); //主排序(第一排序)
 
        ComparatorChain multiSort = new ComparatorChain(sortFields);
        Collections.sort (sortList , multiSort);
         
        return sortList;
    }
     
    /**
     * 对list进行排序
     * @param sortList 需要排序的list
     * @param param1   排序的参数名称:参数长度
     * @param param2   排序的参数名称:排序参数
     * @param orderType 排序类型:正序-asc;倒序-desc  
     */
    public static List sortParam2(List sortList, String param1,String param2, String orderType){
        Comparator mycmp1 = ComparableComparator.getInstance ();
        Comparator mycmp2 = ComparableComparator.getInstance ();
        if("desc".equals(orderType)){
            mycmp1 = ComparatorUtils. reversedComparator(mycmp1); //逆序(默认为正序)
        }
         
        ArrayList<Object> sortFields = new ArrayList<Object>();
        sortFields.add( new BeanComparator(param1 , mycmp1)); //主排序(第一排序)
        sortFields.add( new BeanComparator(param2 , mycmp2)); //主排序(第一排序)
 
        ComparatorChain multiSort = new ComparatorChain(sortFields);
        Collections.sort (sortList , multiSort);
         
        return sortList;
    }
     
    public static List testMapSort(){
        List sortList = new ArrayList();
         
        Map map = new HashMap();
        map.put("name", "1");
        map.put("age", "1");
         
        Map map2 = new HashMap();
        map2.put("name", "2");
        map2.put("age", "13");
         
        Map map1 = new HashMap();
        map1.put("name", "2");
        map1.put("age", "12");
         
        List list = new ArrayList();
        list.add(map);
        list.add(map1);
        list.add(map2);
         
        //return sort(list, "age", "asc");
        return sortParam2(list, "name", "age", "asc");
    }
     
}

 

转载于:https://www.cnblogs.com/zuge/p/6854587.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值