Are there actions or extensions for sorting arrays/structures?

I’m wanting to know how to sort an array of structures, and the only information I’ve found in the forum seems to indicate that you have to do this manually, by implementing your own sorting algorithm.

Is there not an included action or extension that can just sort an array or structure for you?

Hi, there is the ArrayTools extension that also offers a sort-function for numbers in an array. Maybe it is not exactly what you are looking for but some of the things you can do with it might be helpful.

1 Like

Thanks @Drona. I was searching for ‘sort’, not ‘array’, so I missed that one.

have the same issue with sorting an array alphabetically (simple player list).
The Array Tool can only sort numbers.
Javascript has a built-in sort() method.
But sort() seems not to be a part of gdjs…

Has anyone successfully used the sort() method? Thank you

Hi. You can add a suggestion toward the creator of the array tools and ask for a text sort. I looked at the Javascript for the number sort. I changed the GetAsNumber methods to GetAsString and it seemed to work with text.

Text is more complex though. You have upper and lower but then you might also have numbers mixed in and numbers in text form don’t sort in a real world way with the current method.

When treated as text:
Instead of 1,2,3,10,20,30 you get 1,10,2,20,3,30

There does seem to be options with the sort() method to sort mixed text. The sort method would need to be rewritten. It would be helpful to have sort options or flags for things like case and direction.

Hi Keith,
thanks for your tip with changing GetAsNumber methods to GetAsString. Works fine.

Yes, the numbers inside the text is a problem (I have to look for a solution when there is time).
But sorting including lower and upper case letters is no problem. Here is an example:

This is the Original list:

Here the list sorted with ‘GetAsString’:
As expected, first upper then lower case (not so useful)

By using toLowerCase() is the sorting correct A-Z:

And also in Z-A descending order:

1 Like

It’s a nice simple workaround. There are plenty of Google results for sorting natural text or alphanumeric but I haven’t tried any.