java实现集合对象排序(多个字段)

实现了集合的多列升序排序,还没有实现支持中文的排序


import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class MutilOrder implements Comparator<Object> {
    /***************************************************************************
     * @param 比较对象的属性用String
     *            []的形式传过来 比较的对象一定要符合javaBean,即要有Set,Get方法
     */
    String[] fields_user = null;
    boolean isValue;
    //Class fieldType;

    public String[] getFields_user() {
        return fields_user;
    }

    public void setFields(String[] fields_user) {
        this.fields_user = fields_user;
    }

    /**
     * 定义排序规则 如果按照不止一个属性进行排序 这按照属性的顺序进行排序,类是sql order by 即只要比较出同位置的属性就停止
     */
    public int compare(Object obj1, Object obj2) {
        // 没有属性,则不排序
        if (fields_user == null || fields_user.length <= 0) {
            return 2;// 不比较
        }
        for (int i = 0; i < fields_user.length; ) {
            if (compareField(obj1, obj2, fields_user[i])) {
                // 如果obj1的属性值大于obj2的属性值,则返回正数
                return 1;
            } else {
                // 小于返回负数
                return -1;
            }
        }
        // 相等返回0
        return 0;
    }

    /**
     * @param fieldName
     *            根据属性名排序
     */
    private boolean compareField(Object o1, Object o2, String fieldName) {
        Object value1=getFieldValueByName(fieldName, o1);
        Object value2=getFieldValueByName(fieldName, o2);
        if (isValue) {
            try {
                double v1 = ((Number)value1).doubleValue();
                double v2 = ((Number)value2).doubleValue();

                if (v1-v2 > 0) {
                    return true;
                }
            } catch (Exception e) {
                System.err.println("FieldName: " + fieldName
                        + " is not exsited!");
                e.printStackTrace();
            }
            return false;
        } else {
            try {
                String v1 = value1.toString();
                String v2 = value2.toString();

                if (v1.compareTo(v2) > 0) {
                    return true;
                }
            } catch (Exception e) {
                System.err.println("FieldName: " + fieldName
                        + " is not exsited!");
                e.printStackTrace();
            }
            return false;
        }
    }

    /**
     * @param fieldName
     *            属性名 obj 对象 反射获得该属性的值
     */
    public Object getFieldValueByName(String fieldName, Object obj) {
        try {
            Class<? extends Object> objClass = obj.getClass();
            String Letter = fieldName.substring(0, 1).toUpperCase();
            String methodStr = "get" + Letter + fieldName.substring(1);

            Method method = objClass.getMethod(methodStr, new Class[] {});
            Object value = method.invoke(obj, new Object[] {});

            if (value instanceof Number) {
                this.setValue(true);
            }

            return value;
        } catch (Exception e) {
            System.err.println("FieldName:" + fieldName + " is not exsited.");
            return null;
        }
    }

    public boolean isValue() {
        return isValue;
    }

    public void setValue(boolean isValue) {
        this.isValue = isValue;
    }
    public static void main(String[] args) {
        
        List<JavaBean> list = new ArrayList<JavaBean>();
        JavaBean bean = new JavaBean(2,"张三","b");
        list.add(bean);
        bean = new JavaBean(1,"李四","d");
        list.add(bean);
        bean = new JavaBean(3,"王二","c");
        list.add(bean);
        bean = new JavaBean(4,"钱七","a");
        list.add(bean);
        MutilOrder comparator = new MutilOrder();
        comparator.setFields(new String[] {"address"});
        Collections.sort(list, comparator);
        for (JavaBean obj : list) {
            System.out.println(obj.getId()+":"+obj.getName()+":"+obj.getAddress());
        }

    }
}
class JavaBean{
    private String name;
    private String address;
    private int id;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public JavaBean(int id,String name, String address) {
        super();
        this.name = name;
        this.address = address;
        this.id = id;
    }
}

你可能感兴趣的:(集合排序 java)