码迷,mamicode.com
首页 > 编程语言 > 详细

Collections自定义List排序规则

时间:2019-01-08 17:17:16      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:ret   cti   array   arrays   ros   lis   final   java   []   

Collections自定义List排序规则

        //这里的顺序,是我自己定义的一个List<String>
        String[] regulation = {"jams","buke","rose","lua"};
        final List<String> regulationOrder = Arrays.asList(regulation);
        String[] ordered = {"rose","jams","lua","rose","buke","jams","rose","buke","jams"};
        List<String> orderedList = Arrays.asList(ordered);
        Collections.sort(orderedList, new Comparator<String>()
        {
            public int compare(String o1, String o2)
            {
                int io1 = regulationOrder.indexOf(o1);
                int io2 = regulationOrder.indexOf(o2);
                return io1 - io2;
            }
        });
        System.out.println(orderedList);

Collections自定义List排序规则

标签:ret   cti   array   arrays   ros   lis   final   java   []   

原文地址:https://www.cnblogs.com/yoci/p/10239898.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!