Comparing string-based Version Numbers

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I want to compare two version numbers, such as "1.5.1805.2409" with
"1.10.1805.1005". Can I use an operator like "Compare" or "CompareOrdinal"
to do this or do I need to write my own method and compare each number
individually?
 
you can do string compare but you will not know more than that.
if you want more intelligent compare then use something like


Environment.Version.Major;

This will give you more control over what you do with it.
 
I decided to write my own comparison method. I'm posting it here as it may
prove useful to someone in the future:


public static int CompareVersionNumbers(string ver1, string ver2)
{
if (ver1 == ver2)
return 0;

string[] ver1List = ver1.Split(new char[] {'.'});
string[] ver2List = ver2.Split(new char[] {'.'});

for (int i = 0; i < ver1List.Length; i++)
{
int item1 = Convert.ToInt32(ver1List);
int item2 = Convert.ToInt32(ver2List);

if (item1 < item2)
return -1;
else if (item1 > item2)
return 1;

// If the two items are identical then iterate to next pair in the
sequence
}

// Will never reach here but the next statement is needed in order to
compile
return 0;
}
 
you are working too hard. you can just use

Environment.Version.CompareTo(object version)
 
Amigo,

I have two version numbers. Neither of them are the version numbers of a
desktop file. One is stored in the desktop registry. The other is stored on
the Pocket PC's registry.

How does your solution solve this problem?

--
Robert W.
Vancouver, BC
www.mwtech.com



Pohihihi said:
you are working too hard. you can just use

Environment.Version.CompareTo(object version)


Robert W. said:
I decided to write my own comparison method. I'm posting it here as it may
prove useful to someone in the future:


public static int CompareVersionNumbers(string ver1, string ver2)
{
if (ver1 == ver2)
return 0;

string[] ver1List = ver1.Split(new char[] {'.'});
string[] ver2List = ver2.Split(new char[] {'.'});

for (int i = 0; i < ver1List.Length; i++)
{
int item1 = Convert.ToInt32(ver1List);
int item2 = Convert.ToInt32(ver2List);

if (item1 < item2)
return -1;
else if (item1 > item2)
return 1;

// If the two items are identical then iterate to next pair in the
sequence
}

// Will never reach here but the next statement is needed in order to
compile
return 0;
}

 
Sure it does not solve the problem. I assumed that you are talking about
version of an application. I should have not assumed but your question was
not really pointing towards pocketpc and registry, or comparing version
details via a string. Anyways, glad that you solved your problem.


Robert W. said:
Amigo,

I have two version numbers. Neither of them are the version numbers of a
desktop file. One is stored in the desktop registry. The other is stored
on
the Pocket PC's registry.

How does your solution solve this problem?

--
Robert W.
Vancouver, BC
www.mwtech.com



Pohihihi said:
you are working too hard. you can just use

Environment.Version.CompareTo(object version)


Robert W. said:
I decided to write my own comparison method. I'm posting it here as it
may
prove useful to someone in the future:


public static int CompareVersionNumbers(string ver1, string ver2)
{
if (ver1 == ver2)
return 0;

string[] ver1List = ver1.Split(new char[] {'.'});
string[] ver2List = ver2.Split(new char[] {'.'});

for (int i = 0; i < ver1List.Length; i++)
{
int item1 = Convert.ToInt32(ver1List);
int item2 = Convert.ToInt32(ver2List);

if (item1 < item2)
return -1;
else if (item1 > item2)
return 1;

// If the two items are identical then iterate to next pair in
the
sequence
}

// Will never reach here but the next statement is needed in order
to
compile
return 0;
}

 
Robert,

I this this is a nice example of the usage of the String.Split() method.

Regards - Octavio
 
Robert said:

I don't assume this was meant as a friendly salutation, was it? Why do
you think this was called for?
I have two version numbers. Neither of them are the version numbers of a
desktop file. One is stored in the desktop registry. The other is stored on
the Pocket PC's registry.

How does your solution solve this problem?>

The mentioned solution actually does solve the problem. You can easily
initialize a Version object from a string. Like this:

Version v1 = new Version("1.10.1805.1005");
Version v2 = new Version("1.5.1805.2409");
// Now evaluate v1 == v2 to find out they're equal



Oliver Sturm
 
Back
Top