问题描述有一个list集合,包含name,可空。date非空。现在需要排序。需要name是null的排在前面,然后按照date排序问题出现的环境背景及自己尝试过哪些方法list.sort();相关代码//请把代码文本粘贴到下方(请勿用图片代替代码)Listpeople=newArrayList();people.add(newPerson(1,null,sdf.parse("2019-09-2312:12:12")));people.add(newPerson(2,"1",sdf.parse("2019-09-2312:12:12")));people.add(newPerson(3,null,sdf.parse("2019-09-2412:12:12")));people.add(newPerson(4,"1",sdf.parse("2019-09-2412:12:12")));people.add(newPerson(5,null,sdf.parse("2019-09-1712:12:12")));people.add(newPerson(6,null,sdf.parse("2019-09-2412:12:12")));people.add(newPerson(8,null,sdf.parse("2019-09-2012:12:12")));people.add(newPerson(9,null,sdf.parse("2019-09-2312:12:12")));people.add(newPerson(10,"1",sdf.parse("2019-09-2012:12:12")));people.sort((t1,t2)->{if(t1.getName()==null&&t2.getName()!=null){return-1;}elseif(t2.getName()==null&&t1.getName()!=null){return-1;}elseif(Objects.equals(t1.getName(),t2.getName())){returnt1.getEntryDate().compareTo(t2.getEntryDate());}else{returnt1.getName().compareTo(t2.getName());}});System.out.println("SORT===========================");println(people);你期待的结果是什么?实际看到的错误信息又是什么?期望结果:Person[id=5,name=null,entryDate=2019-09-1712:12:12]Person[id=8,name=null,entryDate=2019-09-2012:12:12]Person[id=9,name=null,entryDate=2019-09-2312:12:12]Person[id=1,name=null,entryDate=2019-09-2312:12:12]Person[id=3,name=null,entryDate=2019-09-2412:12:12]Person[id=6,name=null,entryDate=2019-09-2412:12:12]Person[id=10,name=1,entryDate=2019-09-2012:12:12]Person[id=2,name=1,entryDate=2019-09-2312:12:12]Person[id=4,name=1,entryDate=2019-09-2412:12:12]输出结果:SORT===========================Person[id=10,name=1,entryDate=2019-09-2012:12:12]Person[id=5,name=null,entryDate=2019-09-1712:12:12]Person[id=8,name=null,entryDate=2019-09-2012:12:12]Person[id=9,name=null,entryDate=2019-09-2312:12:12]Person[id=4,name=1,entryDate=2019-09-2412:12:12]Person[id=3,name=null,entryDate=2019-09-2412:12:12]Person[id=2,name=1,entryDate=2019-09-2312:12:12]Person[id=1,name=null,entryDate=2019-09-2312:12:12]Person[id=6,name=null,entryDate=2019-09-2412:12:12]
2 回答
森林海
TA贡献2011条经验 获得超2个赞
elseif(t2.getName()==null&&t1.getName()!=null){return1;}第二个if与第一个相反的情况,返回值应该相反,否则顺序会乱
添加回答
举报
0/150
提交
取消