Collection排序工具类--CollectionSortUtil

======================================================Collection排序工具类:

import java.util.Collections;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang3.StringUtils;

public class CollectionSortUtil {
    
    /**
     * 集合工具类汇总:
     * static void reverse(List<?> list):
     *         反转列表中元素的顺序。
     * static void shuffle(List<?> list) :
     *         对List集合元素进行随机排序。
     * static void sort(List<T> list)
     *         根据元素的自然顺序 对指定列表按升序进行排序
     * static <T> void sort(List<T> list, Comparator<? super T> c) :
     *         根据指定比较器产生的顺序对指定列表进行排序。
     * 
     * ****** 适用于List<Integer>
     */
    
    
    /**
     * 学生成绩排序
     * @param stuList
     * @param sortType
     */
    public static <T>void  sortListForStu(List<Student> stuList, String sortType){
        // 正序排列
        if(StringUtils.equals("asc", sortType)){
            Collections.sort(stuList, new ComparatorStuAsc());
        }
        // 倒序排列
        if(StringUtils.equals("desc", sortType)){
            Collections.sort(stuList, new ComparatorStuDesc());
        }
    }
    
    /**
     * 日期列表排序
     * @param dateList
     * @param dateCompare Asc正序 Dsc倒序
     */
    public static <T>void  sortListForDate(List<Date> dateList, Class<T> clazz){
        // 正序排列
        if(clazz == ComparatorDateAsc.class){
            Collections.sort(dateList, new ComparatorDateAsc());
        }
        // 倒序排列
        if(clazz == ComparatorDateDesc.class){
            Collections.sort(dateList, new ComparatorDateDesc());
        }
    }
}
import java.util.Comparator;
import java.util.Date;

public class ComparatorDateAsc implements Comparator<Date>{
    // before 降序排列   after 升序排列
    @Override
    public int compare(Date o1, Date o2) {
        if(o1.getTime() == o2.getTime()){
            return 0;
        }else{
            if(o1.after(o2)){
                return 1;
            }else{
                return -1;
            }
        }
    }
    
}
import java.util.Comparator;
import java.util.Date;

public class ComparatorDateDesc implements Comparator<Date>{
    // before 降序排列   after 升序排列
    @Override
    public int compare(Date o1, Date o2) {
        if(o1.getTime() == o2.getTime()){
            return 0;
        }else{
            if(o1.before(o2)){
                return 1;
            }else{
                return -1;
            }
        }
    }
    
}
import java.util.Comparator;

public class ComparatorStuAsc implements Comparator<Student>{
    
    @Override
    public int compare(Student o1, Student o2) {
        if(o1.getGrade() == o2.getGrade()){
            return 0;
        }else if(o1.getGrade() > o2.getGrade()){
            return 1;
        }else{
            return -1;
        }
    }
}
import java.util.Comparator;

public class ComparatorStuDesc implements Comparator<Student>{
    
    @Override
    public int compare(Student o1, Student o2) {
        if(o1.getGrade() == o2.getGrade()){
            return 0;
        }else if(o1.getGrade() > o2.getGrade()){
            return -1;
        }else{
            return 1;
        }
    }
    
}
import java.util.Comparator;

public class ComparatorStuDesc implements Comparator<Student>{
    
    @Override
    public int compare(Student o1, Student o2) {
        if(o1.getGrade() == o2.getGrade()){
            return 0;
        }else if(o1.getGrade() > o2.getGrade()){
            return -1;
        }else{
            return 1;
        }
    }
    
}

======================================================Collection排序工具测试类:

    /**
     * 学生成绩排序
     */
    @Test
    public void test_sortStuList(){
        Student stu01 = new Student("01001", 70);
        Student stu02 = new Student("01002", 60);
        Student stu03 = new Student("01003", 90);
        List<Student> stuList = new ArrayList<>();
        stuList.add(stu01);
        stuList.add(stu02);
        stuList.add(stu03);
        System.out.println("====================正序排列");
        CollectionSortUtil.sortListForStu(stuList, "asc");
        for(Student stu : stuList){
            String stuMsg = String.format("stuCode=%s, grade=%d", stu.getStuCode(), stu.getGrade());
            System.out.println(stuMsg);
        }
        System.out.println("====================倒序排列");
        CollectionSortUtil.sortListForStu(stuList, "desc");
        for(Student stu : stuList){
            String stuMsg = String.format("stuCode=%s, grade=%d", stu.getStuCode(), stu.getGrade());
            System.out.println(stuMsg);
        }
    }
    
    /**
     * 日期列表排序
     */
    @Test
    public void test_sortDateList(){
        List<Date> dateList = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        dateList.add(calendar.getTime());
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        dateList.add(calendar.getTime());
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        dateList.add(calendar.getTime());
        CollectionSortUtil.sortListForDate(dateList, ComparatorDateAsc.class);
        System.out.println(dateList);
        CollectionSortUtil.sortListForDate(dateList, ComparatorDateDesc.class);
        System.out.println(dateList);
    }
    
    /**
     * 整数列表排序
     */
    @Test
    public void test_sortIntegerList(){
        List<Integer> intList = new ArrayList<>();
        intList.add(10);
        intList.add(50);
        intList.add(60);
        intList.add(30);
        intList.add(40);
        intList.add(20);
        Collections.sort(intList);
        System.out.println(intList);
        Collections.reverse(intList);
        System.out.println(intList);
    }

 

上一篇:C# -- 正则表达式匹配字符之含义


下一篇:题解 CF1599I Desert