Utils.Array.SortByDigits is a new function that takes the given array of strings and runs a numeric sort on it, ignoring any non-digits.

This commit is contained in:
Richard Davey 2020-09-03 15:01:55 +01:00
parent bba8285665
commit 28c6635ea3
2 changed files with 33 additions and 0 deletions

View file

@ -0,0 +1,32 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
/**
* Takes the given array and runs a numeric sort on it, ignoring any non-digits that
* may be in the entries.
*
* You should only run this on arrays containing strings.
*
* @function Phaser.Utils.Array.SortByDigits
* @since 3.50.0
*
* @param {string[]} array - The input array of strings.
*
* @return {string[]} The sorted input array.
*/
var SortByDigits = function (array)
{
var re = /\D/g;
array.sort(function (a, b)
{
return (parseInt(a.replace(re, ''), 10) - parseInt(b.replace(re, ''), 10));
});
return array;
};
module.exports = SortByDigits;

View file

@ -40,6 +40,7 @@ module.exports = {
SendToBack: require('./SendToBack'),
SetAll: require('./SetAll'),
Shuffle: require('./Shuffle'),
SortByDigits: require('./SortByDigits'),
SpliceOne: require('./SpliceOne'),
StableSort: require('./StableSort'),
Swap: require('./Swap')