如何根据列表中的属性对对象列表进行排序
How to sort an objects list based on the attribute in the lists
我有一个对象列表,例如 Notification,它有 3 个列表 - areaWise
、sensorWise
和 regionWise
。我需要通过组合所有 3 个列表,根据最大百分比对通知对象列表进行排序。
下面是我的通知对象:
{
"notificationId": 26,
"areaWise": [],
"sensorWise": [
{
"id": 3,
"threshold": 1,
"headCount": 113,
"pecentage": 11200
},
{
"id": 4,
"threshold": 1,
"headCount": 108,
"pecentage": 10700
},
{
"id": 5,
"threshold": 1,
"headCount": 108,
"pecentage": 10700
},
{
"id": 7,
"threshold": 1,
"headCount": 91,
"pecentage": 9000
}
],
"regionWise": []
},
@Data
public class Item {
private int notificationId;
private List<Wise> areaWise;
private List<Wise> sensorWise;
private List<Wise> regionWise;
@Data
public static class Wise {
private int id;
private int threshold;
private int headCount;
private int pecentage;
}
}
class Test {
static void main() {
ArrayList<Item> items = new ArrayList<>(10);
items.sort(Comparator.comparingInt(
item ->
Stream.of(item.getAreaWise(), item.getSensorWise(), item.getRegionWise())
.flatMapToInt(wises -> wises.stream().mapToInt(Item.Wise::getPecentage))
.max().orElse(Integer.MIN_VALUE)
));
}
}
我有一个对象列表,例如 Notification,它有 3 个列表 - areaWise
、sensorWise
和 regionWise
。我需要通过组合所有 3 个列表,根据最大百分比对通知对象列表进行排序。
下面是我的通知对象:
{
"notificationId": 26,
"areaWise": [],
"sensorWise": [
{
"id": 3,
"threshold": 1,
"headCount": 113,
"pecentage": 11200
},
{
"id": 4,
"threshold": 1,
"headCount": 108,
"pecentage": 10700
},
{
"id": 5,
"threshold": 1,
"headCount": 108,
"pecentage": 10700
},
{
"id": 7,
"threshold": 1,
"headCount": 91,
"pecentage": 9000
}
],
"regionWise": []
},
@Data
public class Item {
private int notificationId;
private List<Wise> areaWise;
private List<Wise> sensorWise;
private List<Wise> regionWise;
@Data
public static class Wise {
private int id;
private int threshold;
private int headCount;
private int pecentage;
}
}
class Test {
static void main() {
ArrayList<Item> items = new ArrayList<>(10);
items.sort(Comparator.comparingInt(
item ->
Stream.of(item.getAreaWise(), item.getSensorWise(), item.getRegionWise())
.flatMapToInt(wises -> wises.stream().mapToInt(Item.Wise::getPecentage))
.max().orElse(Integer.MIN_VALUE)
));
}
}