Java中比较器的使用匿名内部类的写法

1.数组用arrays.sort
2.集合用Collections.sort

数据结构:

"sports": [
            {
                "liveInfo": null,
                "tcount": 3258,
                "picInfo": [
                    {
                        "ref": null,
                        "width": null,
                        "url": "http://cms-bucket.nosdn.127.net/2019/01/07/45021323676a490985570af4af4f3ba5.png",
                        "height": null
                    },
                    {
                        "ref": null,
                        "width": null,
                        "url": "http://cms-bucket.nosdn.127.net/2019/01/07/d5cf033ddc2f4011a07d9c0d87e5152c.png",
                        "height": null
                    },
                    {
                        "ref": null,
                        "width": null,
                        "url": "http://cms-bucket.nosdn.127.net/2019/01/07/6b57e5203fc54981ac7895a406cc3770.png",
                        "height": null
                    }
                ],
                "docid": "E4UQNB3Qbzheng",
                "videoInfo": null,
                "channel": "sports",
                "link": "https://3g.163.com/all/photoview/0005/164691.html",
                "source": "网易体育",
                "title": "大宝救命!跪谢对方门将!国足开门红了",
                "type": "photoset",
                "imgsrcFrom": null,
                "imgsrc3gtype": 2,
                "unlikeReason": "重复、旧闻/6,内容质量差/6",
                "isTop": null,
                "digest": "",
                "typeid": "0B4C0005|164691",
                "addata": null,
                "tag": "图集",
                "category": "推荐",
                "ptime": "2019-01-07 20:59:25"
            },
            {
                "liveInfo": null,
                "tcount": 43488,
                "picInfo": [
                    {
                        "ref": null,
                        "width": null,
                        "url": "http://cms-bucket.nosdn.127.net/2019/01/07/3253cb9696d14c5cbc8032af60717bdd.png",
                        "height": null
                    }
                ],
                "docid": "E4UQ9L4800058780",
                "videoInfo": null,
                "channel": "sports",
                "link": "https://3g.163.com/all/special/S1545887271992.html",
                "source": "网易体育",
                "title": "亚洲杯-吉国门将超级乌龙于大宝反超 国足2-1逆转",
                "type": "special",
                "imgsrcFrom": null,
                "imgsrc3gtype": 1,
                "unlikeReason": "重复、旧闻/6,内容质量差/6",
                "isTop": null,
                "digest": "网易体育1月7日报道:1月7日19点,2019亚洲杯C组首轮",
                "typeid": "S1545887271992",
                "addata": null,
                "tag": "专题",
                "category": "体育",
                "ptime": "2019-01-07 20:51:56"
            },

根据map对象的”ptime”的属性排序

List<Map<String,Object>> sportsLst = (List<Map<String, Object>>) dataMap.get("sports");
            Collections.sort(sportsLst, new Comparator<Map<String, Object>>() {
                @Override
                public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                    try {
                        Date date1 = DateUtil.parseByFormat(o1.get("ptime") + "", "yyyy-MM-dd hh:mm:ss");
                        Date date2 = DateUtil.parseByFormat(o2.get("ptime") + "", "yyyy-MM-dd hh:mm:ss");
                        long date = date1.getTime() - date2.getTime();
                        return (int)date;
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }

若比较器返回的是-1,为递增排列,
若比较器返回的是1,为倒叙排列。

    原文作者:小男人zr
    原文地址: https://blog.csdn.net/weixin_43059031/article/details/86063164
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞