previous iteration in foreach

Good afternoon,

I am trying to compare in a foreach loop a data with the previous iteration, but I am not able to access this iteration, is there any way?

I need to sort a list, but if there are two equal values ​​they have the same order,
with the array
{
name:"aaaaa", valor:2
name:"bbbbb", valor:10
name:"ccccc", valor:10
name:"ddddd", valor:10
name:"eeeee", valor:45
}
get as a result
{
name:"aaaaa", pos:1
name:"bbbbb", pos:2
name:"ccccc", pos:2
name:"ddddd", pos:2
name:"eeeee", pos:3
}

I am trying to save the pos outside of the foreach in a local variable, or recover the value saved in pos of the previous item

Thank you very much

  Discussion posts and replies are publicly visible

  • 0
    Certified Associate Developer
    in reply to Aryan

    to use Aryan code, add this as a new variable right after local!data, and use that new variable:

    todatasubset(
      arrayToPage: local!data,
      pagingConfiguration: a!pagingInfo(
        startIndex: 1,
        batchSize: -1,
        sort: a!sortInfo(
          field: "valor",
          ascending: true
        )
      )
    )

  • 0
    Certified Associate Developer
    in reply to PedroBessa

    if you see name in the code snippet refresh, edited to "valor".

  • 0
    Certified Senior Developer

    This might be using a bit more iterations, but gives the exact out put you expect.

    a!localVariables(
      local!values: {
        { name: "aaaaa", valor: 2 },
        { name: "ddddd", valor: 10 },
        { name: "ccccc", valor: 10 },
        { name: "bbbbb", valor: 10 },
        { name: "eeeee", valor: 45 }
      },
      local!uniqueValor: union(local!values.valor, local!values.valor),
      local!map: a!forEach(
        items: local!uniqueValor,
        expression: todatasubset(
          arrayToPage: a!forEach(
            items: wherecontains(fv!item, tointeger(local!values.valor)),
            expression: a!map(
              name: index(local!values.name, fv!item, null),
              valor: index(local!values.valor, fv!item, null)
            )
          ),
          pagingConfiguration: a!pagingInfo(
            startIndex: 1,
            batchSize: 5000,
            sort: a!sortInfo(field:"name",ascending: true)
          )
        ).data
      ),
      todatasubset(
        arrayToPage: a!flatten(local!map),
        pagingConfiguration: a!pagingInfo(
          startIndex: 1,
          batchSize: 5000,
          sort: a!sortInfo(
            field: "valor",
            ascending: true
          )
        )
      ).data
    )