为了账号安全,请及时绑定邮箱和手机立即绑定

java Comparator为什么没完成排序?

java Comparator为什么没完成排序?

白衣染霜花 2019-02-24 03:58:32
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()); }
查看完整描述

1 回答

?
万千封印

TA贡献1891条经验 获得超3个赞

...
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());
查看完整回答
反对 回复 2019-03-01
  • 1 回答
  • 0 关注
  • 616 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信