关于List排序,简单值排序

package com.tony.test;

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

public class Test {

    public static void main(String[] args) {
        List<TblPowerGroup> list = new ArrayList<TblPowerGroup>();
        list.add(new TblPowerGroup("100008", "4"));
        list.add(new TblPowerGroup("100008", "1"));
        list.add(new TblPowerGroup("100004", "6"));
        list.add(new TblPowerGroup("100004", "5"));
        list.add(new TblPowerGroup("100008", "1"));
        list.add(new TblPowerGroup("100008", "3"));
        List<TblPowerGroup> subList = list.subList(0, 4);
        Map<Object, List<TblPowerGroup>> groupBy = listUtil.groupBy(list, "groupSn");
//        Collections.sort(groupBy.get("100008"), new Comparator<TblPowerGroup>() {
//            @Override
//            public int compare(TblPowerGroup o1, TblPowerGroup o2) {
//                if (o1.getGroupSn().compareTo(o2.getGroupSn()) == 0) {
//                    return o1.getNodeName().compareTo(o2.getNodeName());
//                } else {
//                    return o1.getGroupSn().compareTo(o2.getGroupSn());
//                }
//            }
//        });
        sortList(groupBy.get("100008"));  
        for (TblPowerGroup tblPowerGroup : groupBy.get("100008")) {
        	System.out.println(tblPowerGroup);
			
		}
        
        System.out.println("==============================================");
        for (TblPowerGroup tblPowerGroup : list) {
        	System.out.println(tblPowerGroup);
			
		}
    }

	private static void sortList(List<TblPowerGroup> list) {
			for(int i=0;i<list.size();i++){
				for(int j=0;j<list.size()-i-1;j++){
					if(Integer.valueOf(list.get(j).getNodeName())>Integer.valueOf(list.get(j+1).getNodeName())){
						TblPowerGroup tmp =new TblPowerGroup();
						BeanUtil.copyProperties(list.get(j), tmp);
						BeanUtil.copyProperties(list.get(j+1), list.get(j));
						BeanUtil.copyProperties(tmp,list.get(j+1));
					}
				}
			}
	}
}

class TblPowerGroup {
    private String groupSn;
    private String nodeName;

    
    public TblPowerGroup() {
		super();
	}

	public TblPowerGroup(String groupSn, String nodeName) {
        super();
        this.groupSn = groupSn;
        this.nodeName = nodeName;
    }

    public String getGroupSn() {
        return groupSn;
    }

    public void setGroupSn(String groupSn) {
        this.groupSn = groupSn;
    }

    public String getNodeName() {
        return nodeName;
    }

    public void setNodeName(String nodeName) {
        this.nodeName = nodeName;
    }

    @Override
    public String toString() {
        return "TblPowerGroup [groupSn=" + groupSn + ", nodeName=" + nodeName
                + "]";
    }
}

package com.tony.test;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class listUtil{
	/**
	     * 分组依据接口,用于集合分组时,获取分组
	     * T为要groupBy属性是类型,这个返回值为要groupBy的属性值
	     */
	    public interface GroupBy<T> {
	        T groupBy(Object obj) ;
	    }
	/**
	     * 通过属性对集合分组
	     * @param colls
	     * @param gb
	     * @return
	     * extends Comparable<T> 
	     */
	public static final <T,D> Map<T ,List<D>> groupBy(Collection<D> colls ,GroupBy<T> gb){
	        Map<T ,List<D>> map = new HashMap<T, List<D>>();
	        
	        Iterator<D> iter = colls.iterator() ;
	        
	        while(iter.hasNext()) {
	            D d = iter.next() ;
	            T t = gb.groupBy(d) ;
	            if(map.containsKey(t)) {
	                map.get(t).add(d) ;
	            } else {
	                List<D> list = new ArrayList<D>() ;
	                list.add(d) ;
	                map.put(t, list) ;
	            }
	        }
	        return map ;
	    }
	 /**
	     * 通过属性名称对集合分组
	     * @param colls
	     * @param fieldName为集合中对象的属性名称
	     * @return
	     * extends Comparable<T> 
	     */
	    public static final <T,D> Map<T ,List<D>> groupBy(Collection<D> colls ,final String fieldName){
	        return groupBy(colls,new GroupBy<T>(){
	            @Override
	            public T groupBy(Object obj){
	                Object v=getFieldValueByName(obj,fieldName);
	                return (T)v;
	            }
	        });
	    }
	/** 
	     * 根据属性名称获取属性值 
	     * */  
	   public static Object getFieldValueByName(Object o,String fieldName) {  
	       try {    
	           String firstLetter = fieldName.substring(0, 1).toUpperCase();    
	           String getter = "get" + firstLetter + fieldName.substring(1);    
	           Method method = o.getClass().getMethod(getter, new Class[] {});    
	           Object value = method.invoke(o, new Object[] {});    
	           return value;    
	       } catch (Exception e) {    
	    	   e.printStackTrace();
	           return null;    
	       }    
	   }
	}
package com.tony.test;



import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


public class BeanUtil {


@SuppressWarnings("rawtypes")
public static void copyProperties(Object source, Object target) { 

Class tClass = target.getClass();

Field[] tfields = tClass.getDeclaredFields();
if(tfields.length<=1){
Class tSuperClass = tClass.getSuperclass();
tfields = tSuperClass.getDeclaredFields();
}

Class sClass = source.getClass();
Field[] sfields = sClass.getDeclaredFields();
if(sfields.length<=1){
Class sSuperClass = sClass.getSuperclass();
sfields = sSuperClass.getDeclaredFields();
}
for(int i=0,len = tfields.length;i<len;i++ ){
Field tfield = tfields[i];
if(!tfield.isAccessible()){ 
tfield.setAccessible(true);
      }
      Class<?> tPropertyType = tfield.getType();
      
      if (tPropertyType == String.class){
      
      }else if(tPropertyType == Integer.class) {
      tPropertyType = int.class;
} else if (tPropertyType == Byte.class) {
tPropertyType = byte.class;
} else if (tPropertyType == Short.class) {
tPropertyType = short.class;
} else if (tPropertyType == Float.class) {
tPropertyType = float.class;
} else if (tPropertyType == Double.class) {
tPropertyType = double.class;
} else if (tPropertyType == Character.class) {
tPropertyType = char.class;
} else if (tPropertyType == Long.class) {
tPropertyType = long.class;
} else if (tPropertyType == Boolean.class) {
tPropertyType = boolean.class;
}
      String tFieldName = tfield.getName();
     
for(int j=0, k=sfields.length;j<k;j++ ){ 
Field sfield = sfields[j];
if(!sfield.isAccessible()){ 
sfield.setAccessible(true);
     }
Class<?> sPropertyType = sfield.getType(); 
if (sPropertyType == String.class){

}else if (sPropertyType == Integer.class) {
sPropertyType = int.class;
} else if (sPropertyType == Byte.class) {
sPropertyType = byte.class;
} else if (sPropertyType == Short.class) {
sPropertyType = short.class;
} else if (sPropertyType == Float.class) {
sPropertyType = float.class;
} else if (sPropertyType == Double.class) {
sPropertyType = double.class;
} else if (sPropertyType == Character.class) {
sPropertyType = char.class;
} else if (sPropertyType == Long.class) {
sPropertyType = long.class;
} else if (sPropertyType == Boolean.class) {
sPropertyType = boolean.class;
}
String sFieldName = sfield.getName();
if(tFieldName.equals(sFieldName) && !"serialVersionUID".equals(sFieldName)){
if(sPropertyType == tPropertyType){
String firstLetter = sFieldName.substring(0,1).toUpperCase();
String getMethodName = "get" + firstLetter + sFieldName.substring(1);
try {
Method getMethod = sClass.getMethod(getMethodName, new Class[]{}); 
Object value = getMethod.invoke(source, new Object[]{});
if(value != null){
String setMethodName = "set" + firstLetter + sFieldName.substring(1);
Method setMethod = tClass.getMethod(setMethodName, new Class[]{tfield.getType()});
setMethod.invoke(target, new Object[]{value});
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
}
} 
}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值