A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: How do I do group sorts?

  1. #1
    Senior Member bluemagica's Avatar
    Join Date
    Jun 2008
    Posts
    766

    How do I do group sorts?

    the array contains a bunch of objects, where pairs of objects have a same value for _myId.... Now I want to depth sort the objects in pairs, that is, sort them by their y and keep the objects with same id together. But the function I wrote doesn't seem to be working....any ideas?

    Code:
    private function depthSort():void
    		{
    			var tempArray:Array = gameObjects;
    			tempArray.sortOn(["_myId","y"], [Array.NUMERIC,Array.NUMERIC|Array.DESCENDING]);
    			var i:uint = tempArray.length;
    			while (i--) 
    			{
    				trace(tempArray[i].y + " : " + tempArray[i]._myId+" -> "+i);
    				if (_world.getChildIndex(tempArray[i]) != i)
    				{
    					_world.setChildIndex(tempArray[i],i);
    				}
    			}
    		}
    If you like me, add me to your friends list .

    PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!

    My Arcade My Blog

    Add me on twitter:

  2. #2
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    works for me:

    Code:
    var arr:Array = [];
    
    for (var i:int = 0; i < 200; i++) {
    	
    	arr.push({ id:int(Math.random() * 100), y:Math.random() * 800 });
    }
    
    arr.sortOn(["id", "y"], Array.NUMERIC);
    
    var p:int = 0;
    
    for (i = 0; i < arr.length; i++) {
    	
    	if (arr[i].id != p) {
    		
    		trace("");
    	}
    	
    	trace(arr[i].id + " : " + arr[i].y);
    	
    	p = arr[i].id;
    }
    paste that in flash and run it, you'll see groups of id's together sorted with lowest y to highest.
    lather yourself up with soap - soap arcade

  3. #3
    Senior Member bluemagica's Avatar
    Join Date
    Jun 2008
    Posts
    766
    yeh, that is exactly what I am doing, but it's not working..... my objects are being grouped as per their id, but not sorted as per "y". 1 is is always at index 0, and 2 id is always at index 2....I dunno what I am doing wrong...
    If you like me, add me to your friends list .

    PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!

    My Arcade My Blog

    Add me on twitter:

  4. #4
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    try this sorting option for now:

    tempArray.sortOn(["_myId","y"], Array.NUMERIC);
    lather yourself up with soap - soap arcade

  5. #5
    Senior Member bluemagica's Avatar
    Join Date
    Jun 2008
    Posts
    766
    Already tried it, that was the first thing i did
    If you like me, add me to your friends list .

    PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!

    My Arcade My Blog

    Add me on twitter:

  6. #6
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    maybe post a sample fla then, you've got your "y" property as public? I've heard that using getters for sorting can be buggy.
    lather yourself up with soap - soap arcade

  7. #7
    Senior Member bluemagica's Avatar
    Join Date
    Jun 2008
    Posts
    766
    Well I solved the problem for now, by filtering the array for keeping only one object of a group and then sorting it. I don't know how the filter will hit the performance on the long run though....oh well....Thanks for the help mr.malee
    If you like me, add me to your friends list .

    PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!

    My Arcade My Blog

    Add me on twitter:

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