<p>Here is my code, which takes two version identifiers in the form "1, 5, 0, 4" or "1.5.0.4" and determines which is the newer version.</p>

<p>Suggestions or improvements, please!</p>

<pre><code>    /// &lt;summary&gt;
    /// Compares two specified version strings and returns an integer that 
    /// indicates their relationship to one another in the sort order.
    /// &lt;/summary&gt;
    /// &lt;param name="strA"&gt;the first version&lt;/param&gt;
    /// &lt;param name="strB"&gt;the second version&lt;/param&gt;
    /// &lt;returns&gt;less than zero if strA is less than strB, equal to zero if
    /// strA equals strB, and greater than zero if strA is greater than strB&lt;/returns&gt;
    public static int CompareVersions(string strA, string strB)
    {
        char[] splitTokens = new char[] {'.', ','};
        string[] strAsplit = strA.Split(splitTokens, StringSplitOptions.RemoveEmptyEntries);
        string[] strBsplit = strB.Split(splitTokens, StringSplitOptions.RemoveEmptyEntries);
        int[] versionA = new int[4];
        int[] versionB = new int[4];

        for (int i = 0; i &lt; 4; i++)
        {
            versionA[i] = Convert.ToInt32(strAsplit[i]);
            versionB[i] = Convert.ToInt32(strBsplit[i]);
        }

        // now that we have parsed the input strings, compare them
        return RecursiveCompareArrays(versionA, versionB, 0);
    }

    /// &lt;summary&gt;
    /// Recursive function for comparing arrays, 0-index is highest priority
    /// &lt;/summary&gt;
    private static int RecursiveCompareArrays(int[] versionA, int[] versionB, int idx)
    {
        if (versionA[idx] &lt; versionB[idx])
            return -1;
        else if (versionA[idx] &gt; versionB[idx])
            return 1;
        else
        {
            Debug.Assert(versionA[idx] == versionB[idx]);
            if (idx == versionA.Length - 1)
                return 0;
            else
                return RecursiveCompareArrays(versionA, versionB, idx + 1);
        }
    }
</code></pre>

<hr>

<p>@ <a href="https://stackoverflow.com/questions/30494/compare-version-identifiers#30510">Darren Kopp</a>:</p>

<p>The version class does not handle versions of the format 1.0.0.5.</p>

[{'answer_id': 30508, 'author': 'Craig', 'author_id': 2894, 'author_profile': 'https://Stackoverflow.com/users/2894', 'pm_score': 1, 'selected': False, 'text': '<p>Well, since you only have a four element array you may just want ot unroll the recursion to save time.  Passing arrays as arguments will eat up memory and leave a mess for the GC to clean up later.</p>\n'}, {'answer_id': 30510, 'author': 'Darren Kopp', 'author_id': 77, 'author_profile': 'https://Stackoverflow.com/users/77', 'pm_score': 5, 'selected': False, 'text': '<p>Use the <a href="http://msdn.microsoft.com/en-us/library/system.version.aspx" rel="noreferrer">Version</a> class.</p>\n\n<pre><code>Version a = new Version("1.0.0.0");\nVersion b = new Version("2.0.0.0");\n\nConsole.WriteLine(string.Format("Newer: {0}", (a &gt; b) ? "a" : "b"));\n// prints b\n</code></pre>\n'}, {'answer_id': 30514, 'author': 'Adam Haile', 'author_id': 194, 'author_profile': 'https://Stackoverflow.com/users/194', 'pm_score': 0, 'selected': False, 'text': "<p>If you can assume that each place in the version string will only be one number (or at least the last 3, you can just remove the commas or periods and compare...which would be a lot faster...not as robust, but you don't always need that.</p>\n\n<pre><code>public static int CompareVersions(string strA, string strB)\n{\n    char[] splitTokens = new char[] {'.', ','};\n    string[] strAsplit = strA.Split(splitTokens, StringSplitOptions.RemoveEmptyEntries);\n    string[] strBsplit = strB.Split(splitTokens, StringSplitOptions.RemoveEmptyEntries);\n    int versionA = 0;\n    int versionB = 0;\n    string vA = string.Empty;\n    string vB = string.Empty;\n\n    for (int i = 0; i &lt; 4; i++)\n    {\n        vA += strAsplit[i];\n        vB += strBsplit[i];\n        versionA[i] = Convert.ToInt32(strAsplit[i]);\n        versionB[i] = Convert.ToInt32(strBsplit[i]);\n    }\n\n    versionA = Convert.ToInt32(vA);\n    versionB = Convert.ToInt32(vB);\n\n    if(vA &gt; vB)\n       return 1;\n    else if(vA &lt; vB)\n       return -1;\n    else\n       return 0;  //they are equal\n}\n</code></pre>\n\n<p>And yes, I'm also assuming 4 version places here...</p>\n"}, {'answer_id': 30629, 'author': 'Antti Kissaniemi', 'author_id': 2948, 'author_profile': 'https://Stackoverflow.com/users/2948', 'pm_score': 6, 'selected': True, 'text': '<p>The <a href="http://msdn.microsoft.com/en-us/library/system.version.aspx" rel="nofollow noreferrer">System.Version</a> class does not support versions with commas in it, so the solution presented by <a href="https://stackoverflow.com/questions/30494#30510">Darren Kopp</a> is not sufficient.</p>\n\n<p>Here is a version that is as simple as possible (but no simpler).</p>\n\n<p>It uses <a href="http://msdn.microsoft.com/en-us/library/system.version.aspx" rel="nofollow noreferrer">System.Version</a> but achieves compatibility with version numbers like "1, 2, 3, 4" by doing a search-replace before comparing.</p>\n\n<pre><code>    /// &lt;summary&gt;\n    /// Compare versions of form "1,2,3,4" or "1.2.3.4". Throws FormatException\n    /// in case of invalid version.\n    /// &lt;/summary&gt;\n    /// &lt;param name="strA"&gt;the first version&lt;/param&gt;\n    /// &lt;param name="strB"&gt;the second version&lt;/param&gt;\n    /// &lt;returns&gt;less than zero if strA is less than strB, equal to zero if\n    /// strA equals strB, and greater than zero if strA is greater than strB&lt;/returns&gt;\n    public static int CompareVersions(String strA, String strB)\n    {\n        Version vA = new Version(strA.Replace(",", "."));\n        Version vB = new Version(strB.Replace(",", "."));\n\n        return vA.CompareTo(vB);\n    }\n</code></pre>\n\n<p>The code has been tested with:</p>\n\n<pre><code>    static void Main(string[] args)\n    {\n        Test("1.0.0.0", "1.0.0.1", -1);\n        Test("1.0.0.1", "1.0.0.0", 1);\n        Test("1.0.0.0", "1.0.0.0", 0);\n        Test("1, 0.0.0", "1.0.0.0", 0);\n        Test("9, 5, 1, 44", "3.4.5.6", 1);\n        Test("1, 5, 1, 44", "3.4.5.6", -1);\n        Test("6,5,4,3", "6.5.4.3", 0);\n\n        try\n        {\n            CompareVersions("2, 3, 4 - 4", "1,2,3,4");\n            Console.WriteLine("Exception should have been thrown");\n        }\n        catch (FormatException e)\n        {\n            Console.WriteLine("Got exception as expected.");\n        }\n\n        Console.ReadLine();\n    }\n\n    private static void Test(string lhs, string rhs, int expected)\n    {\n        int result = CompareVersions(lhs, rhs);\n        Console.WriteLine("Test(\\"" + lhs + "\\", \\"" + rhs + "\\", " + expected +\n            (result.Equals(expected) ? " succeeded." : " failed."));\n    }\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/30494', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1490/']