Sorting an array of items by the number in the string item

:robot: Make Bot here bringing solutions to some of your FAQs :robot:

:question: :question::question:

Hi, in one of my scenarios, I need to sort an array containing always the same structure:
filename6.jpg, filename8.jpg, filename5.jpg, filename10.jpg

For now, I used this and it worked for all items below 10:

But when there is a 10 or above, it doesnt work and this is what happens:
filename10.jpg, filename5.jpg, filename6.jpg,filename8.jpg

I understand that this is how javascript works (alphabetical and not numerical sorting) but is there a way to force numerical sorting?

:reply: :reply: :reply:

:nerd_face: If the filename structure is always the same, a potential workaround could be to

  • extract the number of the file as the number item data type
  • add it to a new array of items you need to use further in the flow
  • sort the array with the sort function as the number item data type would be used instead of a string

:one: Extracting the number of the file