package com.palmnest.application.core.util;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * 集合服务帮助类
 *
 * @author yubign
 * @since 2016-07-11
 */
public class CollectionSortMap {

	public static <T> void sortList(List<T> list, final String sortField, final String sortMode) {
		if (list == null || list.size() < 2) {
			return;
		}
		Collections.sort(list, new Comparator<T>() {
			@Override
			public int compare(T o1, T o2) {
				try {
					Map m1 = (Map) o1, m2 = (Map) o2;

					Integer value1 = (Integer) m1.get(sortField);
					Integer value2 = (Integer) m2.get(sortField);

					boolean ASC_order = (sortMode == null || "ASC".equalsIgnoreCase(sortMode));
					return ASC_order ? value1.compareTo(value2) : value2.compareTo(value1);

				} catch (Exception e) {
					String err = e.getLocalizedMessage();
					System.out.println(err);
					e.printStackTrace();
				}

				return 0; //未知类型，无法比较大小
			}
		});
	}

}
