LoginLogin

Alternate uses for (R)SORT

Root / Submissions / [.]

12Me21Created:
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
END
RSORT 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.

No posts yet (will you be the first?)