@@ -23,6 +23,11 @@ var IgeObject = IgeEventingClass.extend({
2323 '_parent' ,
2424 '_children'
2525 ] ;
26+
27+ // Default sorting behavior
28+ this . _sortChildren = function ( compareFn ) {
29+ return this . _children . sort ( compareFn ) ;
30+ }
2631 } ,
2732
2833 /**
@@ -1375,7 +1380,7 @@ var IgeObject = IgeEventingClass.extend({
13751380 arr [ sortObj . order [ i ] ] . depth ( i ) ;
13761381 }
13771382
1378- this . _children . sort ( function ( a , b ) {
1383+ this . _sortChildren ( function ( a , b ) {
13791384 var layerIndex = b . _layer - a . _layer ;
13801385
13811386 if ( layerIndex === 0 ) {
@@ -1390,7 +1395,7 @@ var IgeObject = IgeEventingClass.extend({
13901395
13911396 if ( this . _depthSortMode === 1 ) { // Medium speed, optimised for almost-cube shaped 3d bounds
13921397 // Now sort the entities by depth
1393- this . _children . sort ( function ( a , b ) {
1398+ this . _sortChildren ( function ( a , b ) {
13941399 var layerIndex = b . _layer - a . _layer ;
13951400
13961401 if ( layerIndex === 0 ) {
@@ -1420,7 +1425,7 @@ var IgeObject = IgeEventingClass.extend({
14201425 }
14211426
14221427 // Now sort the entities by depth
1423- this . _children . sort ( function ( a , b ) {
1428+ this . _sortChildren ( function ( a , b ) {
14241429 var layerIndex = b . _layer - a . _layer ;
14251430
14261431 if ( layerIndex === 0 ) {
@@ -1434,7 +1439,7 @@ var IgeObject = IgeEventingClass.extend({
14341439 }
14351440 } else { // 2d mode
14361441 // Now sort the entities by depth
1437- this . _children . sort ( function ( a , b ) {
1442+ this . _sortChildren ( function ( a , b ) {
14381443 var layerIndex = b . _layer - a . _layer ;
14391444
14401445 if ( layerIndex === 0 ) {
@@ -1968,6 +1973,25 @@ var IgeObject = IgeEventingClass.extend({
19681973 }
19691974 } ,
19701975
1976+ /**
1977+ * Gets or sets the function used to sort children for example in depth sorting. This allows us to optionally use
1978+ * a stable sort (for browsers where the native implementation is not stable) or something more specific such as
1979+ * insertion sort for a speedup when we know data is going to be already mostly sorted.
1980+ * @param {Function= } val Sorting function - must operate on this._children and sort the array in place.
1981+ * @example #Set the child sorting algorthm
1982+ * var entity = new IgeEntity();
1983+ * entity.childSortingAlgorithm(function (compareFn) { this._children.sort(compareFn); });
1984+ * @return {* }
1985+ */
1986+ childSortingAlgorithm : function ( val ) {
1987+ if ( val !== undefined ) {
1988+ this . _sortChildren = val ;
1989+ return this ;
1990+ }
1991+
1992+ return this . _sortChildren ;
1993+ } ,
1994+
19711995 /**
19721996 * Returns a string containing a code fragment that when
19731997 * evaluated will reproduce this object.
0 commit comments