【发布时间】:2021-06-18 20:15:48
【问题描述】:
List<ShipInventoryReportDataVO> lstShipInvData = shipInventoryReportDAO
.getShippableInventoryReportData(inputVO, true);
ShipInventoryReportDataVO 是我的对象类并从存储过程中获取数据。在ShipInventoryReportDataVO 对象类中,我有一个名为productCode 的字段,productCode 的数据将以字母数字形式显示
0123,
654,
sparem,
3205,
the wholeland,
10,
1.
当我对对象类中的productCode 进行排序时,我得到的输出为
0123
1
10
3205
654
sparem
wholeland.
这是我尝试排序的代码
List<ShipInventoryReportDataVO> lstShipInvData = shipInventoryReportDAO.getShippableInventoryReportData(inputVO, true);
Collections.sort(lstShipInvData,
(o1, o2) ->
(o1.getProductCode().compareTo(o2.getProductCode()))
);
还有这个
lstShipInvData = (List) lstShipInvData.stream()
.sorted(Comparator.comparing(
ShipInventoryReportDataVO::getProductCode
))
.collect(Collectors.toList());
两个代码都得到如上的输出
但我需要如下输出
1
10
654
0123
3205
sparem
wholeland
这是我现在尝试的代码
List<ShipInventoryReportDataVO> productCodes = shipInventoryReportDAO.getShippableInventoryReportData(inputVO, true);
Comparator<String> byProductCode = Comparator.comparingInt(String::length)
.thenComparing(Comparator.naturalOrder());
productCodes.sort(Comparator.comparing(
ShipInventoryReportDataVO::getProductCode, byProductCode
));
productCodes.forEach(System.out::println);
System.out.println(productCodes.toString());
【问题讨论】:
-
使 ShipInventoryReportDataVO 具有可比性,并在接口实现上编写您的自定义逻辑。
-
无法获得所需的输出
-
@VijayRathod,请编辑并更新您的问题,详细说明哪些问题没有奏效。
-
@AlexRudenko 可能什么都没做,就等你做吧……
-
@m0skit0,很好的尝试咬... :)
标签: java arrays sorting object collections