Home > Enterprise >  Power Automate - Need help sorting an array based on one field that's a number in string format
Power Automate - Need help sorting an array based on one field that's a number in string format

Time:01-23

I have an array of data and all values are in string format due to some prior JSON operations in the flow. I need to sort the array based on one field which are numbers (in string format). I'm using the sort function and it currently sorts the array, but it's done as a string and sorts 'alphabetically' (e.g. "20", "290", "3", "300", "31" instead of the desired "3", "20", "31", "290", "300")

I've tried using Int() and Float() to convert the field to an integer. I'm using the functions wrapped around the mapped item in a select action int(item()?['Points_Total']) with no success. See images.

Both functions give the same error:

The template language function ('int' or 'float') was invoked with a parameter that is not valid. The value cannot be converted to the target type.

The data in the field are whole numbers, both positive and negative. I'd think that both functions could handle the values I'm working with.

Here is how I'm trying to convert the field Points_Total:

enter image description here

Error and View of Data Field

enter image description here

Any ideas from the community?

Thank you!

CodePudding user response:

I'd suggest you have a blank value there somewhere. You'll need to check for that prior to converting to a numeric value. Your expression should be changed to something like this ...

if(isInt(item()?['Points_Total']), int(item()?['Points_Total']), 0)

... my suggestion would be to set it to zero in the cases where it's not a number however, if that skews your results, etc. then you can choose what you do.

Also, I've used isInt given you said the numbers are whole.

  • Related