ArrayUtils
Class to extend the functionality of the ArrayUtil class to add the ability to shuffle the order of the items in an array. I have used this class a few times in projects and it has worked flawlessly every time. It does not matter the array item type, it will shuffle the order. This class only has one method in it. Eventually it may grow. Hope this class helps you as it has helped me.
shuffle(array:Array):Array
Shuffles the items of an array and returns the new array.
- Class version:
- 1.0
- Last Updated:
- July 3, 2009
- Requires:
- Actionscript 3
- Download:
- http://freerksen.googlecode.com/files/flex_arrayutils.zip
Source:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | package com.witheringtree.utils { import mx.utils.ArrayUtil; /** * Utility functions to extend the ArrayUtil class. */ public class ArrayUtils extends ArrayUtil { /** * Shuffles items in an array. */ public static function shuffle(array:Array):Array { var a:Array = []; while(array.length > 0) { a.push(array.splice(Math.round(Math.random() * (array.length - 1)), 1)[0]); } return a; } } } |
Usage:
1 | ArrayUtils.shuffle([1, 2, 3, 4]); |
There are no comments