You can add all parts to lines of a fixed size, then sort them and finally remove the indentation again.
var arr = ['5.5.1', '4.21.0', '4.22.0', '6.1.0', '5.1.0', '4.5.0']; arr = arr.map( a => a.split('.').map( n => +n+100000 ).join('.') ).sort() .map( a => a.split('.').map( n => +n-100000 ).join('.') ); console.log(arr)
Obviously, you should choose the size of the number 100000 wisely: it must have at least one digit more than your largest part of the number will ever be.
With regex
The same manipulation can be achieved without splitting and joining when you use the callback argument for the replace method:
var arr = ['5.5.1', '4.21.0', '4.22.0', '6.1.0', '5.1.0', '4.5.0']; arr = arr.map( a => a.replace(/\d+/g, n => +n+100000 ) ).sort() .map( a => a.replace(/\d+/g, n => +n-100000 ) ); console.log(arr)
Define the fill function only once
Since the fill functions and inverse functions are very similar, it would be nice to use one function f for both functions with an additional argument that defines the "direction" (1 = fill, -1 = expand). This led to this rather obscure and extreme code. Consider this just for fun, not for real use:
var arr = ['5.5.1', '4.21.0', '4.22.0', '6.1.0', '5.1.0', '4.5.0']; arr = (f=>f(f(arr,1).sort(),-1)) ((arr,v)=>arr.map(a=>a.replace(/\d+/g,n=>+n+v*100000))); console.log(arr);
Use the sort callback function
You can use the argument of the sort comparison function to achieve the same:
arr.sort( (a, b) => a.replace(/\d+/g, n => +n+100000 ) .localeCompare(b.replace(/\d+/g, n => +n+100000 )) );
But for large arrays, this will lead to reduced performance. This is because the sorting algorithm often needs to compare a certain value several times, each time with a different value from the array. This means that the filling must be done several times for the same number. For this reason, for large arrays, it will be faster to first apply padding to the entire array, then use standard sorting, and then delete the padding again.
But for shorter arrays, this approach may be the fastest. In this case, the so-called natural sorting option, which can be achieved with the optional localeCompare arguments localeCompare will be more efficient than the fill method:
var arr = ['5.5.1', '4.21.0', '4.22.0', '6.1.0', '5.1.0', '4.5.0']; arr = arr.sort( (a, b) => a.localeCompare(b, undefined, { numeric:true }) ); console.log(arr);
More on padding and single plus
To see how the add-on works, look at the intermediate result that it generates:
[ "100005.100005.100001", "100004.100021.100000", "100004.100022.100000", "100006.100001.100000", "100005.100001.100000" ]
Regarding the expression +n+100000 , note that the first + is a unary plus and is the most efficient way to convert a string decimal number to its numerical equivalent. 100000 is added so that the number has a fixed number of digits. Of course, it can also be 200,000 or 300,000. Note that this add-on does not change the order in which numbers will be arranged in numerical sorting.
Above is just one way to complete a line. See this question and answers for some other alternatives.