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
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)
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", {} ) )
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", {} ) )
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 )
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 ) ) ) ) )
Thanks!! Its working
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:
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.