對象集合、對象數組、數組排序工具類

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.CollationKey;
import java.text.Collator;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.util.ReflectionUtils;

/**
 * 集合工具類
 * 
 * @author Duncan
 * 
 * @d2013-11-20
 */
public class CollectionUtil {

	public final static String SORT_DESC = "desc";
	public final static String SORT_ASC = "asc";

	/**
	 * 判斷集合不爲空
	 * 
	 * @param c
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static boolean isEmpty(Collection c) {
		return c == null || c.size() == 0;
	}

	/**
	 * List集合排序類
	 * 
	 * @param <T>
	 * @param list
	 *            集合
	 * @param property
	 *            排序字段名
	 * @param sortType
	 *            正序 (CollectionUtil.SORT_ASC)、倒序 (CollectionUtil.SORT_DESC)
	 * @param isCN
	 *            是否按中文排序
	 */
	public static <T> void sortList(List<T> list, final String property, final String sortType, final boolean isCN) {
		Collections.sort(list, new Comparator<T>() {
			private Collator collator = null;

			public int compare(T a, T b) {
				int ret = 0;
				Field field = ReflectionUtils.findField(a.getClass(), property);
				String getterMethodName = "get" + StringUtils.capitalize(property);
				Method method = ReflectionUtils.findMethod(a.getClass(), getterMethodName);
				Object value_a = ReflectionUtils.invokeMethod(method, a);
				Object value_b = ReflectionUtils.invokeMethod(method, b);
				if (field.getType() == String.class) {
					if (isCN) {
						collator = Collator.getInstance();
						CollationKey key1 = collator.getCollationKey(value_a.toString());
						CollationKey key2 = collator.getCollationKey(value_b.toString());
						if (sortType != null && sortType.equals(CollectionUtil.SORT_DESC))
							ret = key2.compareTo(key1);
						else
							ret = key1.compareTo(key2);
					} else {
						if (sortType != null && sortType.equals(CollectionUtil.SORT_DESC))
							ret = value_b.toString().compareTo(value_a.toString());
						else
							ret = value_a.toString().compareTo(value_b.toString());
					}
				} else if (field.getType() == Integer.class || field.getType() == Long.class || field.getType() == BigDecimal.class) {
					BigDecimal decA = new BigDecimal(value_a.toString());
					BigDecimal decB = new BigDecimal(value_b.toString());
					if (sortType != null && sortType.equals(CollectionUtil.SORT_DESC))
						ret = decB.compareTo(decA);
					else
						ret = decA.compareTo(decB);
				} else if (field.getType() == Date.class) {
					if (sortType != null && sortType.equals(CollectionUtil.SORT_DESC))
						ret = ((Date) value_b).compareTo((Date) value_a);
					else
						ret = ((Date) value_a).compareTo((Date) value_b);
				}
				return ret;
			}
		});
	}

	/**
	 * 對象數組排序
	 * 
	 * @param <T>
	 * @param array
	 *            對象數組
	 * @param property
	 *            排序字段名
	 * @param sortType
	 *            正序 (CollectionUtil.SORT_ASC)、倒序 (CollectionUtil.SORT_DESC)
	 * @param isCN
	 *            是否按中文排序
	 */
	public static <T> void sortObjectArray(T[] array, final String property, final String sortType, final boolean isCN) {
		Arrays.sort(array, new Comparator<T>() {
			private Collator collator = null;
			public int compare(T a, T b) {
				int ret = 0;
				Field field = ReflectionUtils.findField(a.getClass(), property);
				String getterMethodName = "get" + StringUtils.capitalize(property);
				Method method = ReflectionUtils.findMethod(a.getClass(), getterMethodName);
				Object value_a = ReflectionUtils.invokeMethod(method, a);
				Object value_b = ReflectionUtils.invokeMethod(method, b);
				if (field.getType() == String.class) {
					if (isCN) {
						collator = Collator.getInstance();
						CollationKey key1 = collator.getCollationKey(value_a.toString());
						CollationKey key2 = collator.getCollationKey(value_b.toString());
						if (sortType != null && sortType.equals(CollectionUtil.SORT_DESC))
							ret = key2.compareTo(key1);
						else
							ret = key1.compareTo(key2);
					} else {
						if (sortType != null && sortType.equals(CollectionUtil.SORT_DESC))
							ret = value_b.toString().compareTo(value_a.toString());
						else
							ret = value_a.toString().compareTo(value_b.toString());
					}
				} else if (field.getType() == Integer.class || field.getType() == Long.class || field.getType() == BigDecimal.class) {
					BigDecimal decA = new BigDecimal(value_a.toString());
					BigDecimal decB = new BigDecimal(value_b.toString());
					if (sortType != null && sortType.equals(CollectionUtil.SORT_DESC))
						ret = decB.compareTo(decA);
					else
						ret = decA.compareTo(decB);
				} else if (field.getType() == Date.class) {
					if (sortType != null && sortType.equals(CollectionUtil.SORT_DESC))
						ret = ((Date) value_b).compareTo((Date) value_a);
					else
						ret = ((Date) value_a).compareTo((Date) value_b);
				}
				return ret;
			}
		});
	}

	/**
	 * 數組排序
	 * @param <T>
	 * @param array
	 * 		要排序的數組
	 * @param isCN
	 * 		是否按中文排序
	 * @param sortType
	 * 		正序 (CollectionUtil.SORT_ASC)、倒序 (CollectionUtil.SORT_DESC)
	 */
	public static <T> void sortArray(T[] array, final boolean isCN, final String sortType) {
		if (sortType != null && sortType.equals(CollectionUtil.SORT_DESC)) {
			if (isCN) {
				Arrays.sort(array, Collections.reverseOrder(Collator.getInstance(java.util.Locale.CHINA)));
			} else {
				Arrays.sort(array, Collections.reverseOrder());
			}
		} else {
			if (isCN) {
				Arrays.sort(array, Collator.getInstance(java.util.Locale.CHINA));
			} else {
				Arrays.sort(array);
			}
		}
	}

//	public static void main(String[] args) {
//		User[] arrays = new User[3];
//		User user = new User();
//		user.setId(Long.valueOf(1111));
//		user.setStrName("張三");
//		user.setStrPwd("123456");
//		arrays[0] = user;
//		user = new User();
//		user.setId(Long.valueOf(2222));
//		user.setStrName("李四");
//		user.setStrPwd("234567");
//		arrays[1] = user;
//		user = new User();
//		user.setId(Long.valueOf(3333));
//		user.setStrName("王五");
//		user.setStrPwd("345678");
//		arrays[2] = user;
//		CollectionUtil.sortObjectArray(arrays, "strName", SORT_ASC, true);
//		for (User po : arrays) {
//			System.out.println(po.toString());
//		}
//
//		String[] newArray = { "中山", "汕頭", "廣州", "安慶", "陽江", "南京", "武漢", "北京", "安陽", "北方" };
//		CollectionUtil.sortArray(newArray, true, SORT_DESC);
//		for (String i : newArray) {
//			System.out.print(i + "  ");
//		}
//	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章