Skip to content

Commit fac2efe

Browse files
committed
Container + List.sort scope fixed and custom handler option added. Fix phaserjs#4241
1 parent dc080c0 commit fac2efe

3 files changed

Lines changed: 32 additions & 47 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,8 @@ one set of bindings ever created, which makes things a lot cleaner.
320320
* `Tween.seek` now returns the Tween instance (thanks @rexrainbow)
321321
* `Tween.complete` now returns the Tween instance (thanks @rexrainbow)
322322
* `Tween.stop` now returns the Tween instance (thanks @rexrainbow)
323+
* `List.sort` now has an optional parameter `handler` which allows you to provide your own sort handling function (thanks @jcyuan)
324+
* `Container.sort` now has an optional parameter `handler` which allows you to provide your own sort handling function (thanks @jcyuan)
323325

324326
### Bug Fixes
325327

@@ -373,6 +375,8 @@ one set of bindings ever created, which makes things a lot cleaner.
373375
* Arcade Physics could trigger a `collide` event on a Body even if it performing an overlap check, if the `onCollide` property was true (thanks @samme)
374376
* TileSprites no longer cause a crash when using the Headless mode renderer. Fix #4297 (thanks @clesquir)
375377
* The WebGLRenderer will now apply a transparent background if `transparent = true` in the game config (thanks @gomachan7)
378+
* `List.sort` was missing the scope required for the sort handler, this is now correctly provided internally. Fix #4241 (thanks @jcyuan)
379+
* `Container.sort` was missing the scope required for the sort handler, this is now correctly provided internally. Fix #4241 (thanks @jcyuan)
376380

377381
### Examples and TypeScript
378382

src/gameobjects/container/Container.js

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -525,36 +525,28 @@ var Container = new Class({
525525
* @since 3.4.0
526526
*
527527
* @param {string} property - The property to lexically sort by.
528+
* @param {function} [handler] - Provide your own custom handler function. Will receive 2 children which it should compare and return a boolean.
528529
*
529530
* @return {Phaser.GameObjects.Container} This Container instance.
530531
*/
531-
sort: function (property)
532+
sort: function (property, handler)
532533
{
533-
if (property)
534+
if (!property)
534535
{
535-
this._sortKey = property;
536+
return this;
537+
}
536538

537-
ArrayUtils.StableSort.inplace(this.list, this.sortHandler);
539+
if (handler === undefined)
540+
{
541+
handler = function (childA, childB)
542+
{
543+
return childA[property] - childB[property];
544+
};
538545
}
539546

540-
return this;
541-
},
547+
ArrayUtils.StableSort.inplace(this.list, handler);
542548

543-
/**
544-
* Internal sort handler method.
545-
*
546-
* @method Phaser.GameObjects.Container#sortHandler
547-
* @private
548-
* @since 3.4.0
549-
*
550-
* @param {Phaser.GameObjects.GameObject} childA - The first child to sort.
551-
* @param {Phaser.GameObjects.GameObject} childB - The second child to sort.
552-
*
553-
* @return {integer} The sort results.
554-
*/
555-
sortHandler: function (childA, childB)
556-
{
557-
return childA[this._sortKey] - childB[this._sortKey];
549+
return this;
558550
},
559551

560552
/**

src/structs/List.js

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -183,48 +183,37 @@ var List = new Class({
183183
},
184184

185185
/**
186-
* Sort the contents of this List so the items are in order based
187-
* on the given property. For example, `sort('alpha')` would sort the List
188-
* contents based on the value of their `alpha` property.
186+
* Sort the contents of this List so the items are in order based on the given property.
187+
* For example, `sort('alpha')` would sort the List contents based on the value of their `alpha` property.
189188
*
190189
* @method Phaser.Structs.List#sort
191190
* @since 3.0.0
192191
*
193192
* @genericUse {T[]} - [children,$return]
194193
*
195194
* @param {string} property - The property to lexically sort by.
195+
* @param {function} [handler] - Provide your own custom handler function. Will receive 2 children which it should compare and return a boolean.
196196
*
197197
* @return {Phaser.Structs.List} This List object.
198198
*/
199-
sort: function (property)
199+
sort: function (property, handler)
200200
{
201-
if (property)
201+
if (!property)
202202
{
203-
this._sortKey = property;
203+
return this;
204+
}
204205

205-
StableSort.inplace(this.list, this.sortHandler);
206+
if (handler === undefined)
207+
{
208+
handler = function (childA, childB)
209+
{
210+
return childA[property] - childB[property];
211+
};
206212
}
207213

208-
return this;
209-
},
214+
StableSort.inplace(this.list, handler);
210215

211-
/**
212-
* Internal handler for the {@link #sort} method which compares two items.
213-
*
214-
* @method Phaser.Structs.List#sortHandler
215-
* @private
216-
* @since 3.4.0
217-
*
218-
* @genericUse {T} - [childA,childB]
219-
*
220-
* @param {*} childA - The first item to compare.
221-
* @param {*} childB - The second item to compare.
222-
*
223-
* @return {integer} The result of the comparison, which will be negative if the first item is smaller then second, positive if the first item is larger than the second, or 0 if they're equal.
224-
*/
225-
sortHandler: function (childA, childB)
226-
{
227-
return childA[this._sortKey] - childB[this._sortKey];
216+
return this;
228217
},
229218

230219
/**

0 commit comments

Comments
 (0)