Skip to main content
darkoj
Known Participant
Aug 23, 2021 12:39 PM UTC
Solved

How to get certain object based on Max value.

I have data that is not fetched from DB. 

The Data is in this format: 

List of Dictionary - 2 items

    • Dictionary
        • id - 1(Number (Integer))
          • Max_days - 54 (Number (Integer))
            • Max_amount - 36671(Number (Integer))
            • Dictionary
                • id - 2 (Number (Integer))
                  • Max_days - 1317 (Number (Integer))
                    • Max_amount - 15158 (Number (Integer))

                  What I'm trying to achieve is that I get the whole object that has the highest value for Max_days. So as returning result I should get 

                  • Dictionary
                      • id - 2 (Number (Integer))
                        • Max_days - 1317 (Number (Integer))
                          • Max_amount - 15158 (Number (Integer))

                        Can you help me, guys? Thx

                        csteward
                        cstewardAnswer
                        Brainy
                        Aug 23, 2021 3:12 PM UTC

                        Agree with Stefan, utilize todatasubset() and sort by Max_days, return the top item only:

                        a!localVariables(
                          local!data: {
                            {id: 1, Max_days: 54, Max_amount: 36671},
                            {id: 2, Max_days: 1317, Max_amount: 15158},
                            {id: 3, Max_days: 99, Max_amount: 12545},
                            {id: 4, Max_days: 200, Max_amount: 54321}
                          },
                          
                          index(
                            todatasubset(local!data,a!pagingInfo(1,-1,a!sortInfo("Max_days",false))).data,
                            1,
                            null
                          )
                        )

                        5 replies

                        darkoj
                        darkojAuthor
                        Known Participant
                        Aug 23, 2021 12:45 PM UTC

                        Also to mention this should not be the solution for just these 2 objects as I can have a list of N objects. So the one with the highest Max_days should be returned. Thx

                        stefanhelzle0001
                        Brainy
                        Aug 23, 2021 1:21 PM UTC

                        You can use todatasubset() to sort lists of dictionaries.

                        csteward
                        cstewardAnswer
                        Brainy
                        Aug 23, 2021 3:12 PM UTC

                        Agree with Stefan, utilize todatasubset() and sort by Max_days, return the top item only:

                        a!localVariables(
                          local!data: {
                            {id: 1, Max_days: 54, Max_amount: 36671},
                            {id: 2, Max_days: 1317, Max_amount: 15158},
                            {id: 3, Max_days: 99, Max_amount: 12545},
                            {id: 4, Max_days: 200, Max_amount: 54321}
                          },
                          
                          index(
                            todatasubset(local!data,a!pagingInfo(1,-1,a!sortInfo("Max_days",false))).data,
                            1,
                            null
                          )
                        )