如何根据列表中的属性对对象列表进行排序

huangapple go评论87阅读模式
英文:

How to sort an objects list based on the attribute in the lists

问题

我有一个对象列表,类似于 Notification,其中包含 3 个列表 - areaWisesensorWiseregionWise。我需要根据合并这 3 个列表后的最大百分比对 Notification 对象列表进行排序。

以下是我的通知对象:

{
    "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": []
}
英文:

I have an list of objects like Notification that has 3 lists - areaWise, sensorWise and regionWise. I need to sort the Notification object list based on the max percentage by combining all 3 lists.

Below is my notification object :

{
                "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": []
            },

答案1

得分: 2

@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 percentage;
    }
}

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::getPercentage))
                    .max().orElse(Integer.MIN_VALUE)
        ));
    }
}
英文:
@Data
public class Item {

    private int notificationId;
    private List&lt;Wise&gt; areaWise;
    private List&lt;Wise&gt; sensorWise;
    private List&lt;Wise&gt; regionWise;

    @Data
    public static class Wise {
        private int id;
        private int threshold;
        private int headCount;
        private int pecentage;
    }
}

class Test {
    static void main() {
        ArrayList&lt;Item&gt; items = new ArrayList&lt;&gt;(10);

        items.sort(Comparator.comparingInt(
                item -&gt;
                    Stream.of(item.getAreaWise(), item.getSensorWise(), item.getRegionWise())
                            .flatMapToInt(wises -&gt; wises.stream().mapToInt(Item.Wise::getPecentage))
                .max().orElse(Integer.MIN_VALUE)
        ));
    }
}

huangapple
  • 本文由 发表于 2020年7月30日 02:28:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63160191.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定