0 votes
by (1.1k points)
edited by
Just a little doubt... How to sort an array? I'd like to know how to do this in an array within a for <<for>> as well.
It's a rather silly question but no one really asked that before.

1 Answer

0 votes
by (63.1k points)
selected by
 
Best answer

You should use the array#sort() method. More info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

Example: 

<<set $myArray to [5, 8, 1, 6, 3]>>
<<= $myArray.join(' ')>>
<<set $myArray to $myArray.sort()>>
<<= $myArray.join(' ')>>

You can pass a sorting function to the sort() method if you want it to sort in some specific way, but we'd need more info to provide any examples. 

There's no good reason to use a <<for>> macro for this. 

by (1.1k points)
I would just like to sort an array in alphabetical order. Thanks for the answer. And I'm afraid I have explained wrong, I mean sort an array inside a << for >> and not using one.
by (63.1k points)
The above example will work for alphanumeric order. Just use the sort() method.
...