rich Posted March 10, 2014 Share Posted March 10, 2014 I'm trying to sort an array of sprites based on their y value. The item at the start of the array is drawn first (so at the 'back' of the display list) Lots of sprites will have the same y value, in which case they should not be moved anywhere in the array. For this reason you can't use the native Array.sort (unless there is a compare function I've not yet tested that solves it?) as when items are equal they are shuffled around at will, causing them to constantly flicker as they all swap depths. So.. anyone got some code they could contribute that solves this? Quote Link to comment Share on other sites More sharing options...
Sebi Posted March 10, 2014 Share Posted March 10, 2014 children.sort(function(a,{ if (a.position.y > b.position.y) { return 1; } if (a.position.y < b.position.y) { return -1; } return 0; }); This doesn't work? Same value should stay untouched. Quote Link to comment Share on other sites More sharing options...
rich Posted March 10, 2014 Author Share Posted March 10, 2014 Nope, they don't stay untouched. The above was the first thing I tried. From various answers on twitter it seems I need to use some kind of arbitration value.. a sprite ID, timestamp, etc. Pain in the ass. Quote Link to comment Share on other sites More sharing options...
Geoff Posted March 10, 2014 Share Posted March 10, 2014 I don't have an exact answer for you, but something to look into might be an insertion sort. I've been meaning to try this out because it's supposedly pretty fast for sets that are already mostly sorted and is stable (equal items don't swap). Edit: Our current sorting works like you've mentioned above. We assign IDs to each renderable object upon creation and sub-sort based on that if the depth values are equal:function (a, { return (a.z === b.z) ? (a.id - b.id) : (a.z - b.z);} Quote Link to comment Share on other sites More sharing options...
rich Posted March 10, 2014 Author Share Posted March 10, 2014 Thanks Geoff - yeah I've gone through and applied a z-depth value to all objects and now sort against my criteria + that and it works fine. Quote Link to comment Share on other sites More sharing options...
green_gron Posted March 12, 2014 Share Posted March 12, 2014 You can choose stable sort with better performance than insertion sort O(n2). Almighty wikipedia: Comparison of algorithms Quote Link to comment Share on other sites More sharing options...
d13 Posted March 18, 2014 Share Posted March 18, 2014 sprites.sort(byDepth); function byDepth(a, { if (a.y * a.z < b.y * b.z) { return -1; } else if (a.y * a.z > b.y * b.z) { return 1; } else { return 0; } } Quote Link to comment Share on other sites More sharing options...
Antriel Posted March 18, 2014 Share Posted March 18, 2014 You can choose stable sort with better performance than insertion sort O(n2). Almighty wikipedia: Comparison of algorithms You can hardly find better algo than insertion sort for nearly sorted arrays. Also it always depends on many things and you can't say really until you test it. Looking just at O complexity will not be very precise. Also you will implement insert sort much faster than say heap sort. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.