Skip to main content
suhask0002
February 28, 2022
Question

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

  • February 28, 2022
  • 16 replies
  • 1 view

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.

16 replies

February 28, 2022

a!localVariables(
local!data: "12345698934", /* Number greater than 10 digit */
local!length: len(local!data), 
local!dataToArray: a!forEach(
items: enumerate(local!length),
expression: { tointeger(charat(local!data, fv!index)) }
),
local!sortedData: joinarray(sort(local!dataToArray)),
local!sortedData
)

andrewh0007
February 28, 2022

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",
    {}
  )
)

suhask0002
February 28, 2022

Thanks for the solution

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

can you please check 

andrewh0007
February 28, 2022

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",
    {}
  )
)

stewart.burchell
February 28, 2022

Hi - it'll seem like a long way around but as along as you can guarantee all of your values only contain numerics (i.e. 0 thru 9) then you can:

  • cast each Text item to Decimal
  • generate a Dictionary form this list
  • sort the data
  • cast each Decimal back to Text

If there's any danger that an individual item might not contain all numerics you can test for this whilst looking to cast an individual item and return, say, null to your output array, and then reject all of the null values in your output array.

andrewh0007
February 28, 2022

Definitely like Stewart's approach the most.

a!localVariables(
  local!data: {"3504051867","350405187","350405188","350405189","3604051899","360405189"},
  index(
    index(
      todatasubset(
        a!forEach(
          items: local!data,
          expression: a!map(
            decimalNumber: todecimal(fv!item),
            textNumber: fv!item
          )
        ),
        a!pagingInfo(
          startIndex: 1,
          batchSize: -1,
          sort: a!sortInfo(
            field: "decimalNumber",
            ascending: false
          )
        )
      ),
      "data",
      {}
    ),
    "textNumber",
    {}
  )
)

stewart.burchell
February 28, 2022

...and you can be defensive here as well to exclude any value that inherently isn't a number...