version.go 600 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package utils
  2. import (
  3. "strconv"
  4. "strings"
  5. )
  6. // returns -1 if the first version is lower than the second, 0 if they are equal, and 1 if the second is lower.
  7. func CompareVersions(a, b string) int {
  8. as := strings.Split(a, ".")
  9. bs := strings.Split(b, ".")
  10. var loop int
  11. if len(as) > len(bs) {
  12. loop = len(as)
  13. } else {
  14. loop = len(bs)
  15. }
  16. for i := 0; i < loop; i++ {
  17. var x, y string
  18. if len(as) > i {
  19. x = as[i]
  20. }
  21. if len(bs) > i {
  22. y = bs[i]
  23. }
  24. xi, _ := strconv.Atoi(x)
  25. yi, _ := strconv.Atoi(y)
  26. if xi > yi {
  27. return 1
  28. } else if xi < yi {
  29. return -1
  30. }
  31. }
  32. return 0
  33. }