Last active
June 24, 2016 21:42
-
-
Save virtyaluk/c54ce53a67053842e19f8aa4fa1164ec to your computer and use it in GitHub Desktop.
pseudo-semver sort function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.export = function(v1, v2) { | |
const sv1 = v1.split('.').map(Number), | |
sv2 = v2.split('.').map(Number), | |
res = []; | |
let sv1v = null, | |
sv2v = null; | |
for (let i = 0; i < Math.max(sv1.length, sv2.length); i++) { | |
sv1v = sv1[i] || 0; | |
sv2v = sv2[i] || 0; | |
if (sv1v < sv2v) { | |
return -1; | |
} else if (sv1v > sv2v) { | |
return 1; | |
} | |
} | |
return 0; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const sortFn = require('./pseudosemver-sort'); | |
const arr = ['0.2', '1.2', '1.45.7.5', '0.48', '0.6.7.0', '99.99.99', '99.9.99', '0.0.1', '0.2', '0.2.1'], | |
res = arr.sort(sortFn); | |
console.log(res); | |
/* | |
[ '0.0.1', | |
'0.2', | |
'0.2', | |
'0.2.1', | |
'0.48', | |
'0.6.7.0', | |
'1.2', | |
'1.45.7.5', | |
'99.9.99', | |
'99.99.99' ] | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment