Flash - Custom Array Sorting

How easy is it to make custom sorting code? ever wanted a simple way to randomize an array? here’s a quick and dirty way.

Ok, so I am definitely not saying my way is the best, but it’s still a fast and easy way to customize sorting.


// SIMPLE WAY TO RANDOMIZE
// —————————————————

var array = [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
array.sort ( function (){ return Math.round(Math.random()); } );
trace( array ) // 2,4,1,6,5,3,8,7

// CUSTOM SORT  PICKER
// —————————————————

function arraySortStyle ( style ) : Function
{
          switch ( style )
          {
                    case ‘random’:

                              return function () { return Math.round(Math.random()); };

                    case ‘descending’:

                              return function () { return (( arguments[0] > arguments[1] ) ? 0 : 1); };

                    case ‘ascending’:
                    default:

                              return function () { return (( arguments[0] <  arguments[1] ) ? 0 : 1); };
          }
}

var array = [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

array.sort ( arraySortStyle( ‘random’ ) )
trace( array ) // 3,2,1,5,6,8,7,4

array.sort ( arraySortStyle( ‘descending’ ) )
trace( array ) // 8,7,6,5,4,3,2,1

array.sort ( arraySortStyle( ‘ascending’ ) )
trace( array ) // 1,2,3,4,5,6,7,8

»     » del.icio.us     » Digg It!

Tags: , , , , , , , , ,

5 Comments »

  1. jgraup said,

    February 17, 2007 @ 8:34 pm

    Much faster array sorting - Darron Schall - Custom Code and Info
    http://www.darronschall.com/weblog/archives/000069.cfm

    Array sort() - Sephiroth - Custom Code
    http://www.sephiroth.it/file_detail.php?id=66#

    Array.sort() - ADOBE - Info
    http://livedocs.adobe.com/flash/mx2004/main_7_2/wwhelp/wwhimpl/common/html/wwhelp.htm?context=Flash_MX_2004&file=00001200.html

    Flash Player 7 - Array.sort() overview - Example Code and Info
    http://chattyfig.figleaf.com/pipermail/flashcoders/2003-August/083709.html

    Fast Sorting with Quicksort - Kirupa - Custom Code
    http://www.kirupa.com/developer/actionscript/quickSort.htm

  2. jgraup said,

    February 18, 2007 @ 8:14 pm

    Sorting Algorithms - Animated visual animations (applets)
    http://actionscriptcheatsheet.com/blog/archives/6

  3. jgraup said,

    February 20, 2007 @ 6:33 pm

    ActionScript Object Duplication
    http://theolagendijk.wordpress.com/2006/09/04/actionscript-object-duplication/

  4. jgraup said,

    February 22, 2007 @ 7:51 am

    Array.sortOn does not work with MovieClips
    http://www.connectedpixel.com/blog/gotcha/sorton

  5. jgraup said,

    March 22, 2007 @ 9:58 am

    http://digidood.blogspot.com/2006/08/beta-test.html

RSS feed for comments on this post · TrackBack URI

Leave a Comment

You must be logged in to post a comment.

best kansas city bookstore pirodr! 666