package com.om.utils;

public class VersionComparator {
    public static void main(String[] args) {
        String version1 = "v1.0.1";
        String version2 = "V1.0.2";

        int result = compareVersions(version1, version2);

        if (result < 0) {
            System.out.println(version1 + " 早于 " + version2);
        } else if (result > 0) {
            System.out.println(version1 + " 晚于 " + version2);
        } else {
            System.out.println(version1 + " 等于 " + version2);
        }
    }

    public static int compareVersions(String version1, String version2) {
        String[] arr1 = version1.substring(1).split("\\.");
        String[] arr2 = version2.substring(1).split("\\.");

        int maxLength = Math.max(arr1.length, arr2.length);
        for (int i = 0; i < maxLength; i++) {
            int num1 = (i < arr1.length) ? Integer.parseInt(arr1[i]) : 0;
            int num2 = (i < arr2.length) ? Integer.parseInt(arr2[i]) : 0;

            if (num1 < num2) {
                return -1;
            } else if (num1 > num2) {
                return 1;
            }
        }

        return 0;
    }
}
