Skip to content

Instantly share code, notes, and snippets.

@virtyaluk
Last active June 24, 2016 21:42
Show Gist options
  • Save virtyaluk/c54ce53a67053842e19f8aa4fa1164ec to your computer and use it in GitHub Desktop.
Save virtyaluk/c54ce53a67053842e19f8aa4fa1164ec to your computer and use it in GitHub Desktop.
pseudo-semver sort function
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;
};
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