Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 32113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T13:42:40+00:00 2026-05-10T13:42:40+00:00

Here is my code, which takes two version identifiers in the form 1, 5,

  • 0

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.

Suggestions or improvements, please!

    /// <summary>     /// Compares two specified version strings and returns an integer that      /// indicates their relationship to one another in the sort order.     /// </summary>     /// <param name='strA'>the first version</param>     /// <param name='strB'>the second version</param>     /// <returns>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</returns>     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 < 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);     }      /// <summary>     /// Recursive function for comparing arrays, 0-index is highest priority     /// </summary>     private static int RecursiveCompareArrays(int[] versionA, int[] versionB, int idx)     {         if (versionA[idx] < versionB[idx])             return -1;         else if (versionA[idx] > 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);         }     } 

@ Darren Kopp:

The version class does not handle versions of the format 1.0.0.5.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T13:42:41+00:00Added an answer on May 10, 2026 at 1:42 pm

    The System.Version class does not support versions with commas in it, so the solution presented by Darren Kopp is not sufficient.

    Here is a version that is as simple as possible (but no simpler).

    It uses System.Version but achieves compatibility with version numbers like ‘1, 2, 3, 4’ by doing a search-replace before comparing.

        /// <summary>     /// Compare versions of form '1,2,3,4' or '1.2.3.4'. Throws FormatException     /// in case of invalid version.     /// </summary>     /// <param name='strA'>the first version</param>     /// <param name='strB'>the second version</param>     /// <returns>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</returns>     public static int CompareVersions(String strA, String strB)     {         Version vA = new Version(strA.Replace(',', '.'));         Version vB = new Version(strB.Replace(',', '.'));          return vA.CompareTo(vB);     } 

    The code has been tested with:

        static void Main(string[] args)     {         Test('1.0.0.0', '1.0.0.1', -1);         Test('1.0.0.1', '1.0.0.0', 1);         Test('1.0.0.0', '1.0.0.0', 0);         Test('1, 0.0.0', '1.0.0.0', 0);         Test('9, 5, 1, 44', '3.4.5.6', 1);         Test('1, 5, 1, 44', '3.4.5.6', -1);         Test('6,5,4,3', '6.5.4.3', 0);          try         {             CompareVersions('2, 3, 4 - 4', '1,2,3,4');             Console.WriteLine('Exception should have been thrown');         }         catch (FormatException e)         {             Console.WriteLine('Got exception as expected.');         }          Console.ReadLine();     }      private static void Test(string lhs, string rhs, int expected)     {         int result = CompareVersions(lhs, rhs);         Console.WriteLine('Test(\'' + lhs + '\', \'' + rhs + '\', ' + expected +             (result.Equals(expected) ? ' succeeded.' : ' failed.'));     } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 102k
  • Answers 102k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The __LINE__ macro evaluates to an integer. The _T macro… May 11, 2026 at 8:19 pm
  • Editorial Team
    Editorial Team added an answer It turns out that this is a known inconsistency in… May 11, 2026 at 8:19 pm
  • Editorial Team
    Editorial Team added an answer 3793 after a line count of http://java.sun.com/javase/6/docs/api/allclasses-frame.html (less one for… May 11, 2026 at 8:19 pm

Related Questions

I ran into a scenario where LINQ to SQL acts very strangely. I would
I appear to have a memory leak in this piece of code. It is
I actually have two questions regarding the same problem but I think it is
I'm trying to determine the relative performance of two different queries and have two

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.