compareVersion.js 543 B

12345678910111213141516171819202122232425262728
  1. function compareVersion(v1, v2) {
  2. const $v1 = v1.split('.')
  3. const $v2 = v2.split('.')
  4. const len = Math.max($v1.length, $v2.length)
  5. while ($v1.length < len) {
  6. $v1.push('0')
  7. }
  8. while ($v2.length < len) {
  9. $v2.push('0')
  10. }
  11. for (let i = 0; i < len; i++) {
  12. const num1 = parseInt($v1[i])
  13. const num2 = parseInt($v2[i])
  14. if (num1 > num2) {
  15. return 1
  16. } else if (num1 < num2) {
  17. return -1
  18. }
  19. }
  20. return 0
  21. }
  22. export default compareVersion