Skip to content

Instantly share code, notes, and snippets.

Last active May 10, 2022 04:15
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
A version string comparator that adheres to the SemVer standard (
class SemanticVersionComparator: Comparator<String> {
private val nonDigits: Regex = Regex("[^\\d.]")
private val onPeriods: Regex = Regex("\\.")
override fun compare(currentVersion: String, otherVersion: String): Int {
// remove everything after the "+" character, sanitize non-digits, then split on the periods
val semVerCurrent: MutableList<String> = currentVersion
.replace(nonDigits, "")
val semVerOther: MutableList<String> = otherVersion
.replace(nonDigits, "")
// version numbers not in the form of MAJOR.MINOR.PATCH violate the SemVer standard, so
// add padding zeros to account for legacy conditions (ex: 6.23 -> 6.23.0)
for (i in semVerCurrent.size until 3) {
for (i in semVerOther.size until 3) {
// make a non-mutable defensive copy of the first 3 digits from the list
val current: List<String> = semVerCurrent.subList(0, 3).toList()
val other: List<String> = semVerOther.subList(0, 3).toList()
// iterate through the semantic version digits,
// these lists should be the same length, but use 'minOf' for safety
for(i in 0 until minOf(current.size, other.size)) {
val currentDigit = current[i].toInt()
val otherDigit = other[i].toInt()
// if the two digits aren't equal, compare them and return the comparison result
if(currentDigit != otherDigit) {
return currentDigit.compareTo(otherDigit)
// the two versions are equal
return 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment