简体   繁体   English

在 java 中按多个字段排序列表(然后不比较)

[英]Sort list by multiple fields(not then compare) in java

Now I have an object:现在我有一个 object:

public class Room{
    private long roomId;
    private long roomGroupId;
    private String roomName;
    ... getter 
    ... setter
}

I want sort list of rooms by 'roomId', but in the meantime while room objects has 'roomGroupId' greator than zero and has same value then make them close to each other.我想按“roomId”对房间列表进行排序,但与此同时,房间对象的“roomGroupId”大于零并且具有相同的值,然后使它们彼此靠近。 Let me give you some example:让我举个例子:

input:输入:

[{"roomId":3,"roomGroupId":0},
 {"roomId":6,"roomGroupId":0},
 {"roomId":1,"roomGroupId":1},
 {"roomId":2,"roomGroupId":0},
 {"roomId":4,"roomGroupId":1}]

output: output:

[{"roomId":6,"roomGroupId":0},
 {"roomId":4,"roomGroupId":1},
 {"roomId":1,"roomGroupId":1},
 {"roomId":3,"roomGroupId":0},
 {"roomId":2,"roomGroupId":0}]

As shown above, the list sort by 'roomId', but 'roomId 4' and 'roomId 1' are close together, because they has the same roomGroupId.如上所示,列表按 'roomId' 排序,但 'roomId 4' 和 'roomId 1' 靠得很近,因为它们具有相同的 roomGroupId。

This does not have easy nice solution (maybe I am wrong).这没有简单的好解决方案(也许我错了)。

You can do this like this你可以这样做

        TreeMap<Long, List<Room>> roomMap = new TreeMap<>();

        rooms.stream()
             .collect(Collectors.groupingBy(Room::getRoomGroupId))
             .forEach((key, value) -> {
                 if (key.equals(0L)) {
                     value.forEach(room -> roomMap.put(room.getRoomId(), Arrays.asList(room)));
                 } else {
                     roomMap.put(
                         Collections.max(value, Comparator.comparing(Room::getRoomId))
                                    .getRoomId(),
                         value
                             .stream()
                             .sorted(Comparator.comparing(Room::getRoomId)
                                               .reversed())
                             .collect(Collectors.toList())
                     );
                 }
             });

        List<Room> result = roomMap.descendingMap()
                                   .entrySet()
                                   .stream()
                                   .flatMap(entry -> entry.getValue()
                                                          .stream())
                                   .collect(Collectors.toList());

If you're in Java 8, you can use code like this如果你在 Java 8,你可以使用这样的代码

Collections.sort(roomList, Comparator.comparing(Room::getRoomGroupId)
            .thenComparing(Room::getRoomId));

If not, you should use a comparator如果没有,您应该使用比较器

class SortRoom implements Comparator<Room> 
{ 
 
    public int compare(Room a, Room b) 
    { 
        if (a.getRoomGroupId().compareTo(b.getRoomGroupId()) == 0) {
            return a.getRoomId().compareTo(b.getRoomId());
        }
            
        return a.getRoomGroupId().compareTo(b.getRoomGroupId(); 
    } 
} 

and then use it like this然后像这样使用它

Collections.sort(roomList, new SortRoom()); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM