项目中存在版本的比较:
比如:V3.23.09之前版本走一个逻辑,之后走另外的一个逻辑
fun compareVersions(version1: String, version2: String): Int {val version1Parts = version1.split(".")val version2Parts = version2.split(".")val minLength = minOf(version1Parts.size, version2Parts.size)for (i in 0 until minLength) {val part1 = version1Parts[i].toInt()val part2 = version2Parts[i].toInt()if (part1 < part2) {return -1 // version1 < version2} else if (part1 > part2) {return 1 // version1 > version2}}return version1Parts.size.compareTo(version2Parts.size)}
或者使用hutool的VersionComparator
VersionComparator.INSTANCE.compare(version,targetVersion) > 0
比较的时候是按照字符比较的
class SendServiceTest : FunSpec({test("VersionComparator") {val targetVersion="V3.23.09"VersionComparator.INSTANCE.compare("V3.21.03",targetVersion) shouldBe -2VersionComparator.INSTANCE.compare("V3.22.03",targetVersion) shouldBe -1VersionComparator.INSTANCE.compare("V3.22.03.01",targetVersion) shouldBe -1VersionComparator.INSTANCE.compare("V3.23.06",targetVersion) shouldBe -3VersionComparator.INSTANCE.compare("V3.23.09",targetVersion) shouldBe 0VersionComparator.INSTANCE.compare("V3.23.09.01",targetVersion) shouldBe 1VersionComparator.INSTANCE.compare("V3.23.02",targetVersion) shouldBe -7VersionComparator.INSTANCE.compare("V3.23.12",targetVersion) shouldBe 1}
})