Sorting array by the number

Hello,

I understand this is a well-known topic, but I’m still searching for a solution to my issue.

I am trying to sort a table by a numeric field, however, Make’s ‘sort()’ function sorts alphanumerically rather than numerically. This means that if I input 10, 1, 14, and 4, I get an output of 1, 10, 14, 4.

image

The field I am sorting by is definitely a numeric field. Is there a solution to this problem?

In this article (Sort array by a number), someone suggested a custom module, but it is outdated and no longer works.

Does anyone have any solution?

1 Like

To sort an array of collections by numbers, you cannot use the built-in sort function which sorts by alphanumeric.

What you can do is convert the array into JSON, then use the “Custom JS” module to perform numeric sorting.

Screenshot_2023-11-17_091110

1. Convert the array into JSON using the “Transform to JSON” module

Screenshot_2023-11-17_091158

2. Run JavaScript code to sort the array of collections using the “Custom JS” module

Sample output

Screenshot_2023-11-17_091140

As you can see, output is 1, 4, 10, 14 in ascending order.

4 Likes

Hi @Antoineprpl , great question!

This is not completely true (from what I know), but it fully depends on your data type. If you have a variable which is an integer, it would sort if numerically. You can see it here:


So make sure the numbers you have, are actually integers (by hovering over the output, it shows the data type). In the case these are strings you can use the parseNumber() function

3 Likes

Hello,

Thank you very much for your suggestion to convert the array to JSON and use the “Custom JS” module for numerical sorting. This method indeed seems very effective for sorting arrays of collections by numbers.

However, it’s important to note that this solution involves using a paid tool. In our case, we are looking for a free solution to meet our needs. Do you have any ideas or alternatives that could be used without incurring additional costs?

We greatly appreciate your help and advice, and remain open to other suggestions or ideas.

Thank you again for your time and expertise.

Best regards.

Thank you for pointing out the importance of data types in sorting operations. In my case, I’m confident that the field I’m working with is indeed an integer, as its value is derived from a ParseNumber() function.

Despite this, I’m still encountering the issue where the ‘sort()’ function seems to sort the numbers alphanumerically rather than numerically.

Any further insights or suggestions would be greatly appreciated.

Thanks again for your input!

Can you post a screenshot where you hover over the variable name and it states it’s an integer?

2 Likes

Thank you for your response, and I apologize for my delayed reply. I was at school, as I am an apprentice.

Here is the image showing the type of variable I’m attempting to use for sorting my array:

image

It’s labeled as ‘number’ rather than ‘integer’. I set this value using a ‘set variable’ module derived from a parseNumber() function.

Any suggestions would be greatly appreciated. Thank you in advance!