Skip to main content
darkoj
August 23, 2021
Solved

How to get certain object based on Max value.

  • August 23, 2021
  • 5 replies
  • 0 views

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 - (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 - (Number (Integer))
                        • Max_days - 1317 (Number (Integer))
                          • Max_amount - 15158 (Number (Integer))

                        Can you help me, guys? Thx

                        Best answer by csteward

                        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
                        August 23, 2021

                        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
                        August 23, 2021

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

                        csteward
                        cstewardAnswer
                        August 23, 2021

                        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
                          )
                        )