suchen

Heim  >  Fragen und Antworten  >  Hauptteil

java Comparator为什么没完成排序?

            int top = Integer.parseInt(map.get("top"));
            List<NovelNews> novelNewsList = NovelMongo.GetAllNovel();
            Collections.sort(novelNewsList, new Comparator() {
                public int compare(Object obj0, Object obj1) {
                    NovelNews novel0 = (NovelNews) obj0;
                    NovelNews novel1 = (NovelNews) obj1;
                    return Long.toString(novel1.getHeat()).compareTo(Long.toString(novel0.getHeat()));
                }
            });
                        
            for(int i = 0; i <= top - 1; i++){
                System.out.println(novelNewsList.get(i).getHeat());                
            }
高洛峰高洛峰2766 Tage vor345

Antworte allen(1)Ich werde antworten

  • 阿神

    阿神2017-04-18 10:26:13

    ...
    return Long.toString(novel1.getHeat()).compareTo(Long.toString(novel0.getHeat()));

    String.compareTo(String arg);是按照字典排序的规则,而不是比较值的大小,示例如下:
    23
    2231
    223
    123
    想要按照Long型值的大小排序:

    ...
    return novel0.getHeat().compareTo(novel1.getHeat());

    Antwort
    0
  • StornierenAntwort