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 Reply Children
  • 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
    )

  • 0
    Certified Senior Developer
    in reply to suhask0002

    which is absolutely correct for a text. That is the difficulty of sorting in different types.
    -> i am sure there will be a smoother way, but this code should work.

    a!localVariables(
      local!numbers:{"3504051867","350405187","350405188","350405189","3604051899","360405189"},
      local!map: a!forEach(
        items: local!numbers,
        expression: a!map(
          number:fv!item,
          length: len(fv!item)
        )
      ),
      local!numbersSorted:index(
        todatasubset(
          local!map,
          a!pagingInfo(
            startIndex: 1,
            batchSize: -1,
            sort: a!sortInfo(
              field: "number",
              ascending: false
            )
          )
        ),
        "data",
        {}
      ),
      local!lengths:union(
        tointeger(index(local!numbersSorted,"length",null)),
        tointeger(index(local!numbersSorted,"length",null))
      ),
      a!flatten(
        a!forEach(
        items: local!lengths,
        expression: a!localVariables(
          local!numbers: index(
            local!map,
            wherecontains(
              fv!item,
              tointeger(index(local!map,"length",null))
            ),
            null
          ),
          index(
            index(
              todatasubset(
                local!numbers,
                a!pagingInfo(
                  startIndex: 1,
                  batchSize: -1,
                  sort: a!sortInfo(
                    field: "number",
                    ascending: false
                  )
                )
              ),
              "data",
              {}
            ),
            "number",
            null
          )
        )
      )
      )
    )
    
    


  • 0
    Certified Senior Developer
    in reply to ajhick

    Thanks!! Its working

  • 0
    Certified Lead Developer
    in reply to suhask0002

    Pad all strings with spaces to make text sort working, then sort and remove the spaces.

    a!localVariables(
      local!numbers:{"99", "10", "9999999999999", "3504051867", "1", "350405187","350405188","350405189","3604051899","360405189"},
      trim(
        todatasubset(
          a!forEach(
            items: local!numbers,
            expression: a!map(value: padleft(fv!item, 20))
          ),
          a!pagingInfo(
            startIndex: 1,
            batchSize: -1,
            sort: a!sortInfo(
              field: "value",
              ascending: true
            )
          )
        ).data.value
      )
    )