Quickly sort an array in random order

Here's a quick way to randomly sort an array:

 
# sort { (-1,1)[rand 2] } @array;
 
# example:
my @array = ('foo', 'bar', 'baz', 'wibble');
 
for my $thing (sort { (-1,1)[rand 2] } @array) {
    print "$thing\n";
}

~~DISCUSSION~~

 
perl/randomsort.txt · Last modified: 2010/02/26 10:45 (external edit)
 
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki