Skip to main content
rishukumarg1965
November 27, 2024
Question

Sort the values according category.

  • November 27, 2024
  • 4 replies
  • 1 view

I have a variable that contain list of items, items are ;-

year , category , value

2024, cap internal , 20

2024, cap external, 60

2024, cap material, 50

2025, exp internal , 20

2025, exp external , 30

2025, exp material , 40

I want to sort this like :- 

year , category , value

2024, cap external, 60

2024, cap material, 50

2024, cap internal , 20

2025, exp external , 30

2025, exp material , 40

2025, exp internal , 20

    4 replies

    stefanhelzle0001
    Brainy
    November 27, 2024

    Use the todatasubset() function. Find an example here: https://docs.appian.com/suite/help/24.4/fnc_scripting_todatasubset.html#examples

    gayathris111098
    November 27, 2024

    a!localVariables(
    local!input: {
    {
    "2024, cap internal , 20",
    "2024, cap external, 60",
    "2024, cap material, 50",
    "2025, exp internal , 20",
    "2025, exp external , 30",
    "2025, exp material , 40"
    }
    },
    local!dictionary: a!forEach(
    local!input,
    a!localVariables(
    local!itemVal: split(fv!item, ","),
    a!map(
    year: local!itemVal[1],
    category: local!itemVal[2],
    value: local!itemVal[3]
    )
    )
    ),
    local!sorted: todatasubset(
    local!dictionary,
    a!pagingInfo(
    1,
    - 1,
    {
    a!sortInfo(field: "year", ascending: true()),
    a!sortInfo(field: "value", ascending: false())
    }
    )
    ).data,
    local!sorted
    )

    stefanhelzle0001
    Brainy
    November 27, 2024

    For better readability

    November 27, 2024

    Hello, below is solution for your problem.

    a!localVariables(
      local!category:{
        "2024,cap internal,20",
        "2024,cap external,60",
        "2024,cap material,50",
        "2025,exp internal,20",
        "2025,exp external,30",
        "2025,exp material,40"
      },
      local!data:a!forEach(
        items: local!category,
        expression: split(fv!item,",")
      ),
      local!finalData:
      a!forEach(
        items: local!data,
        expression: 
        a!map(
          "year":fv!item[1],
          "category":fv!item[2],
          "value": fv!item[3]
        )
      ),
      todatasubset(
        local!finalData,
        a!pagingInfo(
          1,
          10,
          a!sortInfo(
            field: "value",
            ascending: false()
          )
        )
      ).data
      
    )

    And if you want to sort by year then just

    change "value" to "year"  in the below code.

    a!sortInfo(
    field: "value",
    ascending: false()
    )