? Alternate uses for (R)SORT ● SmileBASIC Source

Sign In

*Usernames are case-sensitive
Forgot my password

Migrating to a new website for SmileBASIC 4

Alternate uses for (R)SORT

If you pass multiple arrays to SORT, it will sort the first array, then sort the others in the same order. For example, sorting the arrays {3,2,1},{"A","B","C"} would change them to {1,2,3},{"C","B","A"} Shuffling an array: DEF SHUFFLE ARRAY[] VAR I%,LENGTH%=LEN(ARRAY) DIM RAND#[LENGTH%] FOR I%=0 TO LENGTH%-1 RAND#[I%]=RNDF() NEXT I% SORT RAND#,ARRAY[] END This function creates an array, fills it with random values, then sorts it, and sorts the input array in the same order. It's actually faster than SWAPing random elements, (at least for lengths below 100,000) Reversing an array: DEF REVERSE ARRAY[] DIM TEMP%[LEN(ARRAY)] RSORT TEMP%,ARRAY ENDRSORT actually uses the same algorithm as SORT, but it reverses the array at the end. So, if you RSORT an array that is already sorted, it will just reverse it without changing anything.
3 votes
  • sort
  • shuffle
  • randomize
  • random
0 Comment(s)