I think something like this should do the trick:
nums.sort(function(a, b) {
var nums1 = a.split(".");
var nums2 = b.split(".");
for (var i = 0; i < nums1.length; i++) {
if (nums2[i]) { // assuming 5..2 is invalid
if (nums1[i] !== nums2[i]) {
return nums1[i] - nums2[i];
} // else continue
} else {
return 1; // no second number in b
}
}
return -1; // was missing case b.len > a.len
});
Update heres a fiddle
When var nums = ['1.1.1',
'2.6.7.3.2',
'2.6.7',
'2.6.7.3',
'2.6.7.1',
'6.4.23',
'2.7']
Sorting this way => ['1.1.1','2.6.7.1','2.6.7.3.2','2.6.7','2.6.7.3','2.7','6.4.23']
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…