List数组排序

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

public static void main(String[] args) {
        List<Object> list = new ArrayList<>();
        list.add(1);
        list.add(12);
        list.add(122);
        list.add(13);
        list.add(4);
        System.out.println("排序前:"+list);
        Collections.sort(list, new Comparator<Object>() {
            @Override
            public int compare(Object o1, Object o2) {

                if(((Integer)o1 - (Integer)o2)>0){
                    return 1;
                }else {
                    return -1;
                }
            }
        });
        System.out.println("排序后:"+list);
    }

 

posted @ 2019-06-17 15:25  Dkwestworld  阅读(755)  评论(0编辑  收藏  举报