0

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": []
            },
Naman
  • 27,789
  • 26
  • 218
  • 353
Achyut
  • 377
  • 1
  • 3
  • 17
  • 2
    What have you tried so far? – Flown Jul 29 '20 at 21:00
  • I tried using:tpaList.forEach(data -> { data.getNotificationDataSet().stream().map(item -> { Stream.of(item.getAreaWise(),item.getSensorWise(),item.getRegionWise()).sorted(Comparator.comparing(AggregateData::getPecentage)); return item; }).collect(Collectors.toList()); but that sort each list, I need notificationDataSet to be sorted. – Achyut Jul 30 '20 at 03:49
  • When sorting lists you usually implement the comparable interface and then use collections sort. The compare method will have a custom logic that gets the object percentage and then compare it to the percentage of the other object returning which is bigger. Have you tried it? Is there a reason why it won't work here? – Gabriel H Jul 30 '20 at 03:50
  • What do you mean by combining all 3 lists? How are you using areawise and regionwise arrays in your calculations when percentage is only defined in sensorwise? – Sync it Jul 30 '20 at 03:56

1 Answers1

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 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)
        ));
    }
}
a.l.
  • 1,085
  • 12
  • 29
  • Could you please let me know if I have the structure like public class ThresholdPercentageAlert { Integer alertTypeId; //List notificationDataSet; List>> notificationDataSet; and I want to sort the list of map based on the same criteria. – Achyut Aug 07 '20 at 20:15
  • Don't understand your question, and maybe it's a different one, count you post a new question? – a.l. Aug 09 '20 at 14:54