sql中经常用到排序  例如:  select * from emp order by filed1 asc , filed2 asc , filed3 desc ;现在有一个类 Entity   它有 三个属性  int filed1  , int filed2 , int filed3 .如果给一个 Entity[] entitys  集合给你 , 然后按照指定的顺序进行排序 ,
 比如 : 
filed1 升序 filed2 降序  filed3 升序
或者
filed1 降序或则filed3 升序   filed2 降序你要怎么实现?  还有 ,当Entity属性越来越多时 又要怎么做? 要通用一点。

解决方案 »

  1.   

    闲来无事帮你写的一个简单的annotation来表明升序还是降序
    import java.lang.annotation.*;@Target(ElementType.FIELD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface Sort {
    String order() default "ASC";
    }
    实现一个comparatorimport java.lang.reflect.Field;
    import java.util.Comparator;public class EntityComparator implements Comparator { @Override
    public int compare(Object o1, Object o2) {
    if (!(o1 instanceof Entity && o2 instanceof Entity))
    return 0;

    Field[] fields = o1.getClass().getFields();
    String s1 = "";
    String s2 = "";
    for(Field field : fields){
    String order = field.getAnnotation(Sort.class).order();
    if (null == order)
    continue;

    try {
    int i1 = field.getInt(o1);
    int i2 = field.getInt(o2);
    //如果降序,反转两个值
    if ("DESC".equals(order)){
    i1 *= -1;
    i2 *= -1;
    }
    //通过比较各个field的值填充两个string,最后比较两个string就行了
    if (i1 > i2){
    s1 += "2";
    s2 += "0";
    }
    else if (i1 == i2){
    s1 += "1";
    s2 += "1";
    }
    else {
    s1 += "0";
    s2 += "2";
    }
    } catch (IllegalArgumentException e) {
    continue;
    } catch (IllegalAccessException e) {
    continue;
    }
    }
    return s1.compareTo(s2);
    }
    }Entity类及测试import java.util.Arrays;
    import java.util.Comparator;public class Entity { @Sort(order = "ASC")
    public int field1;

    @Sort(order = "ASC")
    public int field2;

    @Sort(order = "DESC")
    public int field3;

    public Entity(int v1, int v2, int v3){
    field1 = v1;
    field2 = v2;
    field3 = v3;
    }

    public String toString(){
    return "" + field1 + field2 + field3;
    } public static void main(String[] args){
    Entity[] es = new Entity[3];
    es[0] = new Entity(1,2,2);
    es[1] = new Entity(2,2,3);
    es[2] = new Entity(2,2,2);
    Arrays.sort(es, new EntityComparator());
    for(Entity e : es){
    System.out.println(e);
    }
    }
    }
      

  2.   

    上面的用annotation规定顺序逆序不能动态的改变
    重新弄了一个import java.lang.reflect.Field;
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.Map;public class Entity { @Sort()
    public int field1;
    @Sort()
    public int field2;
    @Sort()
    public int field3;

    private static Map<String, String> order = null;

    static{
    order = new HashMap<String,String>();
    Field[] fields = Entity.class.getFields();
    for(Field field : fields){
    Sort s = field.getAnnotation(Sort.class);
    if (null == s) 
    continue;
    order.put(field.getName(), "ASC");
    }
    }

    public static void changeFieldSortOrder(String fieldName, String orderStr){
    if (order.containsKey(fieldName)){
    if ("ASC".toLowerCase().equals(orderStr.toLowerCase()))
    order.put(fieldName, "ASC");
    else {
    order.put(fieldName, "DESC");
    }
    }
    }

    public static String getFieldSortOrder(String fieldName){
    if (order.containsKey(fieldName)){
    return order.get(fieldName);
    }
    else
    return "ASC";
    }

    public Entity(int v1,int v2, int v3){
    field1 = v1;
    field2 = v2;
    field3 = v3;
    }

    public String toString(){
    return "" + field1 + field2 + field3;
    }

    public static void main(String[] args){
    Entity[] es = new Entity[3];
    es[0] = new Entity(1,1,1);
    es[1] = new Entity(2,2,3);
    es[2] = new Entity(2,2,2);
    Arrays.sort(es, new EntityComparator());
    for(Entity e : es){
    System.out.println(e);
    }
    Entity.changeFieldSortOrder("field1", "DESC");
    Arrays.sort(es, new EntityComparator());
    for(Entity e : es){
    System.out.println(e);
    }
    }
    }
    import java.lang.reflect.Field;
    import java.util.Comparator;public class EntityComparator implements Comparator { @Override
    public int compare(Object o1, Object o2) {
    if (!(o1 instanceof Entity && o2 instanceof Entity))
    return 0;

    Field[] fields = o1.getClass().getFields();
    String s1 = "";
    String s2 = "";
    for(Field field : fields){
    String order = Entity.getFieldSortOrder(field.getName());
    if (null == order)
    continue;

    try {
    int i1 = field.getInt(o1);
    int i2 = field.getInt(o2);
    //如果降序,反转两个值
    if ("DESC".equals(order)){
    i1 *= -1;
    i2 *= -1;
    }
    //通过比较各个field的值填充两个string,最后比较两个string就行了
    if (i1 > i2){
    s1 += "2";
    s2 += "0";
    }
    else if (i1 == i2){
    s1 += "1";
    s2 += "1";
    }
    else {
    s1 += "0";
    s2 += "2";
    }
    } catch (IllegalArgumentException e) {
    continue;
    } catch (IllegalAccessException e) {
    continue;
    }
    }
    return s1.compareTo(s2);
    }
    }