List按某个字段排序

方式一:

需要jdk1.8以上

List<User> newList = list.stream().sorted(Comparator.comparing(User::getAge))
                .collect(Collectors.toList());

方式二:

import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class SortList<T> {

    /**
     * @param targetList
     *            目标排序List
     * @param sortField
     *            排序字段
     * @param sortMode
     *            排序方式
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public void sort(List<T> targetList, final String sortField, final String sortMode) {
        Collections.sort(targetList, new Comparator() {
            
            public int compare(Object obj1, Object obj2) {
                int retVal = 0;
                try {
                    Method method1 = ((T) obj1).getClass().getMethod(sortField, null);
                    Method method2 = ((T) obj2).getClass().getMethod(sortField, null);
                    if (sortMode != null && "desc".equals(sortMode)) {
                        retVal = method2.invoke(((T) obj2), null).toString()
                                .compareTo(method1.invoke(((T) obj1), null).toString()); // 倒序
                    } else {
                        retVal = method1.invoke(((T) obj1), null).toString()
                                .compareTo(method2.invoke(((T) obj2), null).toString()); // 正序
                    }
                } catch (Exception e) {
                    throw new RuntimeException();
                }
                return retVal;
            }
            
        });
    }

}

相关博客:Java List 按某字段排序 支持:字符串(String)、日期(Date)、整型(Integer)、整形(Long)