Does anyone have code to compare two version numbers in JavaScript? I just want simple version comparisons (e.g. "1.0"
vs "1.5.6"
), and it should work with numbers or strings. It can ignore trailing beta identifiers like "1.5.6b4"
, but can otherwise expect the strings to be well-formed. The function should return a signed integer like a normal cmp function.
function cmpVersion(a, b)
return less than one if a < b
return 0 if a == b
return greater than one if a > b
I have an answer, but will choose a better or more elegant solution over my own.
(I am using this to compare jQuery.browser.version
numbers, but the answer will be more widely applicable)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…