Skip to content

sortNumeric JavaScript

When sorting an array consisting of strings, it sorts first by the numbers contained in the strings, not by their names. For example, given the array ['1-a', '100-a', '10-a', '2-a'], it returns ['1-a', '2-a', '10-a', '100-a'] with the smaller numbers at the front.

Parameters

  • array::string[]
  • descending::boolean

Returns

string[]

Examples

javascript
sortNumeric(['a1a', 'b2a', 'aa1a', '1', 'a11a', 'a3a', 'a2a', '1a']);
// Returns ['1', '1a', 'a1a', 'a2a', 'a3a', 'a11a', 'aa1a', 'b2a']

Released under the MIT License