I have a number greater than 10 digit and that stored as a text Is there any way to sort them?

Certified Senior Developer

I have number greater than 10 digit and that stored as a text and  I want sort them. I have tried with sortextarray() that is sorting based on ascii values so it is not giving me correct output. and I tried to convert into int but that is giving me infinity Please help me here to sort the array.

Thanks in advance.

  Discussion posts and replies are publicly visible

Parents
  • If you mean you have a CDT/Dictionary array of data and you want to sort ascending (or descending) on that field then you can use the following code.

    a!localVariables(
      local!numberOfEntries: 10,
      local!data: a!forEach(
        items: 1 + enumerate(local!numberOfEntries),
        expression: {
          id: fv!item,
          number: concat(round(rand(12) * 9, 0))
        }
      ),
      index(
        todatasubset(
          local!data,
          a!pagingInfo(
            startIndex: 1,
            batchSize: -1,
            sort: a!sortInfo(
              field: "number",
              ascending: true
            )
          )
        ),
        "data",
        {}
      )
    )

  • 0
    Certified Senior Developer
    in reply to ajhick

    Thanks for the solution

    This is not working for input {"3504051867","350405187","350405188","350405189","3604051899","360405189"}

    can you please check 

  • a!localVariables(
      local!data: {"3504051867","350405187","350405188","350405189","3604051899","360405189"},
      local!convertDataToDictionary: a!forEach(
        items: local!data,
        expression: {
          number: fv!item
        }
      ),
      index(
        todatasubset(
          local!convertDataToDictionary,
          a!pagingInfo(
            startIndex: 1,
            batchSize: -1,
            sort: a!sortInfo(
              field: "number",
              ascending: true
            )
          )
        ),
        "data",
        {}
      )
    )

  • 0
    Certified Senior Developer
    in reply to ajhick

    I have sorted in  descending order and The highlighted number should be the second number but it is sorting based on ascii value so it is giving wrong sorting order.

  • Ah. Yes. I only really looked when they were all the same length. I'll have a think.

    Edit: It's starting to feel a little clunky but here it is. I'll also update the code above just in case anyone ever uses it

    a!localVariables(
      local!data: {"3504051867","350405187","350405188","350405189","3604051899","360405189"},
      local!maxNumberLength: max(len(local!data)),
      local!convertDataToDictionary: a!forEach(
        items: local!data,
        expression: a!localVariables(
          local!length: len(fv!item),
          local!numberOfLeadingZeros: tointeger(local!maxNumberLength - local!length),
          {
            number: fv!item,
            numberWithLeadingZeros: concat(
              repeat(local!numberOfLeadingZeros, 0),
              fv!item
            )
          }
        )
      ),
      local!orderedNumbers: index(
        index(
          todatasubset(
            local!convertDataToDictionary,
            a!pagingInfo(
              startIndex: 1,
              batchSize: -1,
              sort: a!sortInfo(
                field: "numberWithLeadingZeros",
                ascending: false
              )
            )
          ),
          "data",
          {}
        ),
        "number",
        {}
      ),
      local!orderedNumbers
    )

Reply
  • Ah. Yes. I only really looked when they were all the same length. I'll have a think.

    Edit: It's starting to feel a little clunky but here it is. I'll also update the code above just in case anyone ever uses it

    a!localVariables(
      local!data: {"3504051867","350405187","350405188","350405189","3604051899","360405189"},
      local!maxNumberLength: max(len(local!data)),
      local!convertDataToDictionary: a!forEach(
        items: local!data,
        expression: a!localVariables(
          local!length: len(fv!item),
          local!numberOfLeadingZeros: tointeger(local!maxNumberLength - local!length),
          {
            number: fv!item,
            numberWithLeadingZeros: concat(
              repeat(local!numberOfLeadingZeros, 0),
              fv!item
            )
          }
        )
      ),
      local!orderedNumbers: index(
        index(
          todatasubset(
            local!convertDataToDictionary,
            a!pagingInfo(
              startIndex: 1,
              batchSize: -1,
              sort: a!sortInfo(
                field: "numberWithLeadingZeros",
                ascending: false
              )
            )
          ),
          "data",
          {}
        ),
        "number",
        {}
      ),
      local!orderedNumbers
    )

Children