java 通用对象排序

一个排序类,一个排序util?

no、no、no……

使用反射机制,写了一个通用的对象排序util,欢迎指正。

实体类:

package entity;

public class BaseTypeEntity {
    public BaseTypeEntity(int i, String sg, double d, float f, short st, long l, char c) {
        super();
        this.i = i;
        this.sg = sg;
        this.d = d;
        this.f = f;
        this.st = st;
        this.l = l;
        this.c = c;
    }
    @Override
    public String toString() {
        return "BaseTypeEntity [i=" + i + ", sg=" + sg + ", d=" + d + ", f=" + f + ", st=" + st + ", l=" + l + ", c="
                + c + "]";
    }
    private int i;
    private String sg;
    private double d;
    private float f;
    private short st;
    private long l;
    private char c;
    
    public int getI() {
        return i;
    }
    public void setI(int i) {
        this.i = i;
    }
    public String getSg() {
        return sg;
    }
    public void setSg(String sg) {
        this.sg = sg;
    }
    public double getD() {
        return d;
    }
    public void setD(double d) {
        this.d = d;
    }
    public float getF() {
        return f;
    }
    public void setF(float f) {
        this.f = f;
    }
    public short getSt() {
        return st;
    }
    public void setSt(short st) {
        this.st = st;
    }
    public long getL() {
        return l;
    }
    public void setL(long l) {
        this.l = l;
    }
    public char getC() {
        return c;
    }
    public void setC(char c) {
        this.c = c;
    }
    
}

排序类(重点--圈一下,考试要考):

package util;

import java.lang.reflect.Field;
import java.util.Comparator;

@SuppressWarnings("rawtypes")
public class ComparatorUtil implements Comparator{
    //要排序的属性名
    private String name;
    //是否升序
    private boolean falg = true;
    /**
     * @param name 要排序的属性名,如果是基本类型的排序,请入null值
     * @param falg false/true : 升序/降序
     */
    public ComparatorUtil(String name, boolean falg) {
        super();
        this.name = name;
        this.falg = falg;
    }

    @Override
    public int compare(Object o1, Object o2) {
        int result = 0;
        //入参为空|参数类型不相等,都不进行处理。
        if(o1 == null || o2 == null || !o1.getClass().getName().equals(o2.getClass().getName())){
            return result;
        }
        //7大基本类型的处理(boolean除外.且把Collections.reverse()/Collections.sort()的事也做了) 
        if(isBaseType(o1)){
            //比较
            return baseTypeOpt(o1,o2);
        }
        try {
            Field f1 = o1.getClass().getDeclaredField(name);
            Field f2 = o2.getClass().getDeclaredField(name);
            //设置private可读
            f1.setAccessible(true);
            f2.setAccessible(true);
            result = baseTypeOpt(f1.get(o1),f2.get(o2));
        }  catch (Exception e) { //异常懒得处理了,如果没有对应的属性,那就不排序了.(手动滑稽)
            e.printStackTrace();
        }
        return result;
    }

    private int baseTypeOpt(Object o1, Object o2) {
        int result = 0;
        if(o1 instanceof String){
            result = o1.toString().compareTo(o2.toString());
        }else if(o1 instanceof Integer){
            result = ((Integer)o1) - ((Integer)o2);
        }else if(o1 instanceof Double){
            if(((Double)o1 - (Double)o2) > 0){
                result = 1;
            }else if(((Double)o1 - (Double)o2) < 0){
                result = -1;
            }
        }else if(o1 instanceof Float){
            if(((Float)o1 - (Float)o2) > 0){
                result = 1;
            }else if(((Float)o1 - (Float)o2) < 0){
                result = -1;
            }
        }else if(o1 instanceof Character){
            result = ((Character)o1).compareTo(((Character)o2));
        }else if(o1 instanceof Short){
            result = ((Short)o1) - ((Short)o2);
        }else if(o1 instanceof Long){
            if(((Long)o1 - (Long)o2) > 0){
                result = 1;
            }else if(((Long)o1 - (Long)o2) < 0){
                result = -1;
            }
        }
        //降序
        if(isFalg()){
            result = -result;
        }
        return result;
    }

    private boolean isBaseType(Object o) {
        if((o instanceof String) || (o instanceof Integer)
                || (o instanceof Double) || (o instanceof Float) 
                || (o instanceof Character) || (o instanceof Short)
                || (o instanceof Long)){
            return true;
        }
        return false;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isFalg() {
        return falg;
    }

    public void setFalg(boolean falg) {
        this.falg = falg;
    }
    
}

测试类:

package util;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import entity.BaseTypeEntity;
import entity.User;

public class MainTest {
    
    public static void main(String[] args) {
        //基本类型排序
        baseTypeSort();
        //对象排序
        objectSort();
    }
    
    private static void baseTypeSort(){
        List<String> ss = new ArrayList<String>();
        ss.add("aa");
        ss.add("bc");
        ss.add("ac");
        ss.add("ba");
        ss.add("dd");
        ss.add("da");
        System.out.println("before sort:");
        System.out.println(ss);
        System.out.println("sort end");
        //倒序
        Collections.reverse(ss);
        System.out.println(ss);
    }
    
    private static void objectSort(){
        List<BaseTypeEntity> btes = new ArrayList<BaseTypeEntity>();
        BaseTypeEntity bte = new BaseTypeEntity(1, "abc", 12d, 15f, (short) 3, 600l, 'd');
        btes.add(bte);
        bte = new BaseTypeEntity(2, "aba", 122d, 152f, (short) 2, 602l, 'a');
        btes.add(bte);
        bte = new BaseTypeEntity(3, "aac", 132d, 132f, (short) 1, 3l, 'a');
        btes.add(bte);
        System.out.println("before sort:");
        for(BaseTypeEntity b : btes){
            System.out.println(b);
        }
        //开始排序
        Collections.sort(btes,new ComparatorUtil("d",true));
        System.out.println("sort end");
        for(BaseTypeEntity b : btes){
            System.out.println(b);
        }
    }
    
}

(少于150个字,不让发布)

来点人生感悟,凑字数。

7月初刚刚换好工作,个人感觉还是进大公司好,各方面很规范。之前的小公司,就没有规范一说了。业务不行了,就解散了整个研发部,然后没有按照国家的法律来走,我还差一个月就工作满一年了,才补贴半个月……还强迫我们签离职协议,不签就不补。

语气强硬的说:就补这么多,不服可以去劳动局闹,我们不怕。-------确实牛哈

欢迎评论、指正。

原文地址:https://www.cnblogs.com/chen--biao/p/7243954.html