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();
                    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章