美文网首页
list元素降序排列

list元素降序排列

作者: 上进的小二狗 | 来源:发表于2018-11-20 10:00 被阅读0次

题目:

给定一个List,里面存放了5个顺序打乱的Integer数字,要求通过重写Collections的sort方法将集合里面的元素按照从大到小进行排序并输出

分析:

通过重写Collections的sort方法

/**
 * 
 * @author liyao
 *
 * 给定一个List,里面存放了5个顺序打乱的Integer数字,要求通过重写Collections的sort方法将集合里面的元素按照从大到小进行排序并输出
 */
public class ArrayListSortTest {
    public static void main(String[] args) {
         List<Integer> list = new ArrayList<Integer>(){
             {
                 add(121);
                 add(111);
                 add(131);
                 add(99);
                 add(181);
             }
        };
         
        Collections.sort(list,new Comparator<Integer>(){
            @Override
            public int compare(Integer o1, Integer o2) {
                if(o1 > o2){
                    return -1;
                }else{          
                    return 1;
                }
            }
        });
        System.out.println(list);
        
    }    
}

相关文章

网友评论

      本文标题:list元素降序排列

      本文链接:https://www.haomeiwen.com/subject/vqzmfqtx.html