Tech Tip: Shuffle array order
PRODUCT: 4D | VERSION: 14.3 | PLATFORM: Mac & Win
Published On: June 16, 2015
Below is an utility method that will shuffle the order of elements in an array.
// Method: UTIL_Shuffle_Array // $1 - Pointer to array to be shuffled // ---------------------------------------------------- C_POINTER($1;$arr) C_LONGINT($i) If(Count parameters>=1) $arr:=$1 ARRAY LONGINT($randArr;Size of array($arr->)) For ($i;1;Size of array($arr->)) $randArr{$i}:=Random End for SORT ARRAY($randArr;$arr->) End if |
Example:
ARRAY LONGINT($arr;0) For ($i;1;10) APPEND TO ARRAY($arr;$i) End for UTIL_Shuffle_Array (->$arr) |
Below are the result of the array elements shuffled:
![](https://kb.4d.com:443/kbase/assets/images/77311-1.jpg)