A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: numberArray.sort(function(){..} - DallasNYC

  1. #1
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176

    numberArray.sort(function(){..} - DallasNYC

    this is where i first came across the "harlem shuffle" -
    http://groups.google.com/group/macro...b71816b688db0f
    for a fuller explanation, try googling for years as i have done ( if you ever find one, let me know ) or try to make sense of trace outs -
    Code:
    arr1=[1,2,3,4,5,6];;
    arr2 = [1,2,3,4,5,6,7,8,9];
    
    function shuffle(){
    nVal = Math.floor(Math.random()*3)-1;
    return nVal;
    };
    
    chk = 0;
    
    function tracer(){
    ++chk>arr2.length ? clearInterval(traceInt) : null;
    num = Math.floor(Math.random()*3)-1;
    arr1.sort(num); 
    trace("num = "+num+" returns - "+arr1);
    arr2.sort(shuffle);
    trace("nVal = "+nVal+" returns - "+arr2); 
    }
    
    traceInt = setInterval(tracer,50);

  2. #2
    Senior Member
    Join Date
    Oct 2003
    Posts
    1,354
    Thanks Dog, I'm slowly understanding it, but not 100%.

    From flash help:
    public sort([compareFunction:Object], [options:Number]) : Array
    My first mistake was thinking that just by putting a number (-1, 0,or 1)for the parameter would give the same result.
    Code:
    myArray.sort(-1)
    However, this will only result in sending the second '[options:number]' parameter. Probally defaults to 1, since -1 and 0 are not options.

    For the three numbers (1, 0, -1) to work, we must use a function as the first parameter.
    Then by adding a trace within that function, you will see that the sort method actaully calls on the function several times..
    Code:
    numberArray = [];
    for (var n = 0; n != 4; n++) {//using only 4 to reduce the possible results
    	numberArray.push(n);
    }
    trace("original = "+numberArray);
    randNum = function () {
    	nVal = Math.floor(Math.random()*3)-1;
    	trace(nVal);
    	return nVal;
    };
    numberArray.sort(randNum);
    trace("random = "+numberArray);
    traces out..
    original = 0,1,2,3
    1
    -1
    -1
    1
    1
    random = 1,2,0,3
    The amount of times the sort method calls the function varies depending on the previous comparison. Since the sort method sends two values to the function, we can trace them out to get a better idea of what is going on..
    Code:
    numberArray = [];
    for (var n = 0; n != 4; n++) {
    	numberArray.push(n);
    }
    trace("original = "+numberArray);
    randNum = function (a, b) {
    	trace("-------------");
    	trace("comparing: "+a+":"+b);
    	nVal = Math.floor(Math.random()*3)-1;
    	trace("using: "+nVal);
    	return nVal;
    };
    numberArray.sort(randNum);
    trace("random = "+numberArray);
    At this point, I'm still not clear how the sort method picks which two items in the array to compare. But I'm atleast to the point that I understand that we are telling flash to use a random comparison for each pair.

    Thanks..

  3. #3
    Senior Member
    Join Date
    Oct 2003
    Posts
    1,354
    Actaully, we dont need to use -1,0,1. We can return any positive or any negative. This works well for sorting numbers correctly. If we use sort() without any parameters, we get a wacky list, because flash puts '10' before '2'. But if we use our own function, we can use some simple math to let flash know the right order..

    Code:
    numberArray = [];
    for (var n = 0; n != 20; n++) {
    	numberArray.push(n);
    }
    trace("original = "+numberArray);
    randNum = function (a, b, c) {
    	nVal = Math.floor(Math.random()*3)-1;
    	return nVal;
    };
    numberArray.sort(randNum);
    trace("random = "+numberArray);
    numberArray.sort();
    trace("In order? = "+numberArray);
    sortAscending = function (a, b) {
    	return a-b;
    };
    numberArray.sort(sortAscending);
    trace("Really in Order = "+numberArray);
    O.K. so now sort(function) is really coming together for me..

  4. #4
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    Glad to hear it's coming together

    I still don't fully understand it, but your explanation throws some light on the internal workings

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center