How to sort a CDT from another CDT source?

Certified Senior Developer

Hi,

I've already had to sort a CDT and I could use this code below very useful.

However, how would you do to sort a CDT from another CDT source (without Query the DB)?

Example : I need to sort the cdt1 on the cdtDocs.order field

cdt1
  - id
  - ref
  - label
  - idDoc (references the cdtDocs.id field)


cdtDocs
  - id
  - order
  - label

cast(
  typeof(local!cdt),
  todatasubset(
    local!cdt,
    a!pagingInfo(
      startIndex: 1,
      batchSize: -1,
      sort: a!sortInfo(
        field: local!field,
        ascending: local!ascending
      )
    )
  ).data
)

  Discussion posts and replies are publicly visible

Parents
  • Here's one way I can think of, but it's kinda messy - basically you have to define a new variable with the ID of the first CDT and the order from the second CDT. Then, you sort it in the same way you showed above. Lastly, you restructure your original array using the changed sort order.

    a!localVariables(
      local!data: {
        a!map(
          id: 1,
          name: "Name 1",
          nestedFieldId: 101
        ),
        a!map(
          id: 2,
          name: "Name 2",
          nestedFieldId: 102
        ),
        a!map(
          id: 3,
          name: "Name 3",
          nestedFieldId: 100
        )
      },
      local!orderData: {
        a!map(
          nestedId: 100,
          nestedName: "Test",
          order: 2
        ),
        a!map(
          nestedId: 101,
          nestedName: "Test again",
          order: 3
        ),
        a!map(
          nestedId: 102,
          nestedName: "Test another time",
          order: 1
        )
      },
      local!dataWithSortOrder: a!forEach(
        items: local!data,
        expression: a!map(
          id: fv!item.id,
          order: displayvalue(
            fv!item.nestedFieldId,
            local!orderData.nestedId,
            local!orderData.order,
            0
          )
        )
      ),
      local!sortedDataIds: todatasubset(
        local!dataWithSortOrder,
        a!pagingInfo(
          startIndex: 1,
          batchSize: -1,
          sort: a!sortInfo(
            field: "order",
            ascending: true
          )
        )
      ),
      a!forEach(
        items: local!sortedDataIds,
        expression: displayvalue(
          fv!item.id,
          local!data.id,
          local!data,
          {}
        )
      )
    )

    I'd probably only recommend this if you had a small number of items - generally the database is more efficient at sorting data, so it's good to use the capabilities from the DB if you can. Just curious - what is your use case? How are you using the data?

  • 0
    Certified Senior Developer
    in reply to Peter Lewis

    Thanks a lot Peter.
    Yes, it is for a small list of records... but also to learn to use this kind of trick ;-)

    We get the data at the beggining but these are sorted on another field. Then we transmit these data in different interfaces. For some interfaces we need to sort that list on another field...
    We can query the DB, but it is maybe superfluous, as we already have the data in our CDT.

Reply
  • 0
    Certified Senior Developer
    in reply to Peter Lewis

    Thanks a lot Peter.
    Yes, it is for a small list of records... but also to learn to use this kind of trick ;-)

    We get the data at the beggining but these are sorted on another field. Then we transmit these data in different interfaces. For some interfaces we need to sort that list on another field...
    We can query the DB, but it is maybe superfluous, as we already have the data in our CDT.

Children
No Data