6 Replies - 588 Views - Last Post: 30 November 2016 - 05:42 PM Rate Topic: -----

#1 CoolCell6   User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 20-November 16

Best way to get the max version number from array?

Posted 30 November 2016 - 03:47 PM

I understand how to get the max number from a int array:

int[] myNumbers = new int[3];
myNumbers[0] = 9;
myNumbers[1] = 2;
myNumbers[2] = 3;

MessageBox.Show(Convert.ToString(myNumbers.Max())); //Result = 9


What is the best way to break down and get the max VersionNumber from the array?

string[] VersionNumbers = new string[3];
VersionNumbers[0] = "2.0.1966";
VersionNumbers[0] = "3.0.2400";
VersionNumbers[0] = "2.1.1329";



Thank you in advance...

Is This A Good Question/Topic? 0
  • +

Replies To: Best way to get the max version number from array?

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6536
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Best way to get the max version number from array?

Posted 30 November 2016 - 04:08 PM

'Best' is relative.
You could split them on the '.'
Then sort. The last item in the sort is the highest.
Was This Post Helpful? 1
  • +
  • -

#3 CoolCell6   User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 20-November 16

Re: Best way to get the max version number from array?

Posted 30 November 2016 - 04:26 PM

That helps me with a good approach to do this solve and a direction to focus on to solve my issue. Sometimes it does help just to have a another view on the problem. Thank you.

Split them would work. but getting your view help me reliaze I can remove the '.' and convert the result to int then get the int max and maybe make the code shorter.

Thank you for helping me look in a different light.

Your awesome tlhIn`toq
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7189
  • View blog
  • Posts: 24,366
  • Joined: 05-May 12

Re: Best way to get the max version number from array?

Posted 30 November 2016 - 04:54 PM

Use the built in .NET Framework Version class. Since Max() uses IComparable<T>, and Version implements IComparable<T>, you have a match made in heaven.

VersionNumberStrings.Select(s => new Version(s)).Max()


Was This Post Helpful? 2
  • +
  • -

#5 CoolCell6   User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 20-November 16

Re: Best way to get the max version number from array?

Posted 30 November 2016 - 05:00 PM

WOW! Thank you! I didn't know about version class going to look it up and learn more about it. Thanks!

This post has been edited by CoolCell6: 30 November 2016 - 05:01 PM

Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Best way to get the max version number from array?

Posted 30 November 2016 - 05:36 PM

If you still have to deal with Strings (like, wherever you're reading the versions in from happen to be strings), it's fairly easy to Parse them and find their Max, all in a single statement. LINQ makes a lot of things really simple like this, once you start thinking the way it wants you to.

var versionNumbers = new []{ "2.0.1966", "3.0.2400",  "2.1.1329"};	
var max = versionNumbers.Select(s => Version.Parse(s)).Max();
Console.WriteLine("Max: {0}", max);


Of course, for real code, you'd want some error checking instead of just trusting that Version.Parse worked, but that's the gist of things.
Was This Post Helpful? 1
  • +
  • -

#7 CoolCell6   User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 20-November 16

Re: Best way to get the max version number from array?

Posted 30 November 2016 - 05:42 PM

Thank you. Curtis Rutland glad to see your code mine was close to yours what I figured out but yours was shorter. I've learned so much since I joined this forum thank you Guys and Gals.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1