0
List<ShipInventoryReportDataVO> lstShipInvData = shipInventoryReportDAO
    .getShippableInventoryReportData(inputVO, true);

ShipInventoryReportDataVO is my object class and getting the data from a stored procedure. In ShipInventoryReportDataVO object class I have a field called productCode and the data of productCode will in alphanumeric like

0123, 
654, 
sparem, 
3205, 
the wholeland, 
10, 
1. 

when I am sorting the productCode which is in object class, I am getting the output as

0123
1
10
3205
654
sparem
wholeland.

this is the code I tried to sort

List<ShipInventoryReportDataVO> lstShipInvData = shipInventoryReportDAO.getShippableInventoryReportData(inputVO, true);
                
Collections.sort(lstShipInvData, 
    (o1, o2) -> 
        (o1.getProductCode().compareTo(o2.getProductCode()))
);

and this

lstShipInvData = (List) lstShipInvData.stream()
        .sorted(Comparator.comparing(
            ShipInventoryReportDataVO::getProductCode
        ))
        .collect(Collectors.toList());

both the codes gets output as above

but I need the output as below

1
10
654
0123
3205
sparem
wholeland

This is the code which i tried now

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());

You can wrap your query in set;

distinct() does not go well with values() as per documentation

ip_sets = set(Comment.objects.order_by().values('ip_address'))
ip_list = list(set(Comment.objects.order_by().values('ip_address')))
4

1 回答 1

0

预期的输出显示数据应首先按产品代码的长度排序,然后按字母顺序排序。

这可以通过链接比较器和使用来实现Comparator.comparing(Function<? super T,? extends U> keyExtractor, <? super U> keyComparator)

List<ShipInventoryReportDataVO> productCodes = Arrays.asList(
        new ShipInventoryReportDataVO("0123"),
        new ShipInventoryReportDataVO("654"),
        new ShipInventoryReportDataVO("sparem"),
        new ShipInventoryReportDataVO("3205"),
        new ShipInventoryReportDataVO("the wholeland"),
        new ShipInventoryReportDataVO("10"),
        new ShipInventoryReportDataVO("1")
);

Comparator<String> byProductCode = Comparator.comparingInt(String::length)
    .thenComparing(Comparator.naturalOrder());

productCodes.sort(Comparator.comparing(
    ShipInventoryReportDataVO::getProductCode, byProductCode
));

productCodes.forEach(System.out::println);

输出

1
10
654
0123
3205
sparem
the wholeland
于 2021-03-22T08:47:57.760 回答