mirror of
https://github.com/photonstorm/phaser
synced 2025-02-17 06:28:30 +00:00
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:
parent
bba8285665
commit
28c6635ea3
2 changed files with 33 additions and 0 deletions
32
src/utils/array/SortByDigits.js
Normal file
32
src/utils/array/SortByDigits.js
Normal 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;
|
|
@ -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')
|
||||
|
|
Loading…
Add table
Reference in a new issue