對List中的資料根據時間排序-倒序
阿新 • • 發佈:2019-02-20
private static void ListSort(List<News> list) { Collections.sort(list, new Comparator<News>() { @Override public int compare(News n1, News n2) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); try { Date dt1 = format.parse(Jsoup.parse(new String(n1.getCrTime())).text()); Date dt2 = format.parse(Jsoup.parse(new String(n2.getCrTime())).text()); if (dt1.getTime() > dt2.getTime()) { return -1; } else if (dt1.getTime() < dt2.getTime()) { return 1; } else { return 0; } } catch (Exception e) { e.printStackTrace(); } return 0; } }); }