list自定义排序工具类

工具类

package sort;
import java.lang.reflect.Method;
import java.text.Collator;
import java.util.Comparator;
import java.util.Date;

/**
 * @Title: LocalComparator.java
 * @Package
 * @Description: TODO(用一句话描述该文件做什么)
 * @author licy
 * @date 2018年11月8日
 * @version V1.0
 */

public class LocalComparator implements Comparator{
	private Comparator cmp = Collator.getInstance(java.util.Locale.CHINA);

	/**
	 * 排序字段
	 **/

	private String[] sortBy;

	/*
	 * 字段对应排序类型 (1:升序,-1:降序) 一一对应
	 */
	private int[] order;

	public LocalComparator(String[] sortBy, int[] order) {
		this.sortBy = sortBy;
		this.order = order;
	}

	/*
	 * 定义排序规则 如果按照不止一个属性进行排序 这按照属性的顺序进行排序,类似sql order by 即只要比较出同位置的属性就停止
	 */
	public int compare(Object o1, Object o2) {
		for (int i = 0; i < sortBy.length; i++) {
			Object value1 = getFieldValueByName(sortBy[i], o1);
			Object value2 = getFieldValueByName(sortBy[i], o2);
			if (value1 instanceof Integer && value2 instanceof Integer) {
				int v1 = Integer.parseInt(value1.toString());
				int v2 = Integer.parseInt(value2.toString());
				if (v1 > v2) {
					return 1 * this.order[i];
				} else if (v1 < v2) {
					return -1 * this.order[i];
				}
			} else if (value1 instanceof Long && value2 instanceof Long) {
				Long v1 = (Long) value1;
				Long v2 = (Long) value2;
				if (v1 > v2) {
					return 1 * this.order[i];
				} else if (v1 < v2) {
					return -1 * this.order[i];
				}
			} else if (value1.getClass() == Date.class && value2.getClass() == Date.class) {
				Date time1 = (Date) value1;
				Date time2 = (Date) value2;
				if (time1.getTime() > time2.getTime()) {
					return 1 * this.order[i];
				} else if (time1.getTime() < time2.getTime()) {
					return -1 * this.order[i];
				}
			} else {
				int result = cmp.compare(value1, value2);
				if (result != 0) {
					return result * this.order[i];
				}
			}
		}
		return 1;
	}

	/**
	 * 
	 * 通过反射,获取属性值
	 * 
	 * @param fieldName
	 * @param o
	 * @return
	 */
	private Object getFieldValueByName(String fieldName, Object o) {
		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) {
			System.out.println("属性不存在");
			return null;
		}
	}
}

  

 

测试类

package sort.test;

/**
 * @Title: Persion.java
 * @Package sort.test
 * @Description: TODO(用一句话描述该文件做什么)
 * @author licy
 * @date 2018年11月8日
 * @version V1.0
 */

public class Persion {
	
	private String name;
	private int age;

	public String getName() {
		return name;
	}

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

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public Persion() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Persion(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}

}

  

 

执行测试类

package sort.test;

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

import org.junit.Test;

import sort.LocalComparator;

public class ComparatorTest {

	@SuppressWarnings("unchecked")
	@Test
	public void test() {
		List list = new ArrayList();
		Persion p1 = new Persion("a", 3);
		Persion p2 = new Persion("b", 2);
		Persion p3 = new Persion("b", 1);
		list.add(p1);
		list.add(p2);
		list.add(p3);
		String[] sortBy = new String[] { "name", "age" };
		int[] order = new int[] { 1, -1 };
		LocalComparator lc = new LocalComparator(sortBy, order);
		Collections.sort(list, lc);
		for(int i=0;i<list.size();i++) {
			Persion p = (Persion) list.get(i);
			System.out.println(p.getName() + ":" + p.getAge());
		}
	}

}

  

 

转载于:https://www.cnblogs.com/lichangyunnianxue/p/9933660.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值