-5

How to get the largest value from collection of strings using c# and LINQ ? example:

List<String> lst = new List<String> { "1.0.0", "1.0.2", "1.0.15", "1.1.0" };

Example 1 :- compare 1.0.0 and 1.1.0, the largest version is 1.1.0.
Example 2 :- compare 1.0.2 and 1.0.15, the largest version is 1.0.15.
Example 3 :- compare 1.0.0 and 1.0.2, the largest version is 1.0.2.

sebu
  • 2,824
  • 1
  • 30
  • 45
Howard Teoh
  • 75
  • 3
  • 8
  • I think the following question will help you: https://stackoverflow.com/a/8288096/4648564 – ronmar May 15 '18 at 06:11
  • 2
    Aside from their being multiple duplicates from a simple google search for this... what have you tried? – Sayse May 15 '18 at 06:17

4 Answers4

5

Consider using .Net Version class

You can create it from a string, and it already handles comparisons in a way that is possibly what you need

    Version v1 = new Version("1.2.3.4");
    Version v2 = new Version("1.3.0");
    Console.WriteLine(v2>v1);

In your case, using Linq, you can have

    List<String> lst = new List<String> { "1.0.0", "1.0.2", "1.0.15", "1.1.0" };

    Version maxVersion = lst.Select(s=> new Version(s)).Max();
    Console.WriteLine(maxVersion);
Gian Paolo
  • 4,161
  • 4
  • 16
  • 34
  • @HowardTeoh: Note that you can simplify even this slightly: `Version maxVersion = lst.Max(s => Version.Parse(s));`. – Jon Skeet May 15 '18 at 06:24
2

In the example you've given, you could just sort them as strings. But I'd strongly advise you not to do that - otherwise "11.0.0" would be seen as earlier than "2.0.0".

Instead, I'd suggest creating a Version class with major/minor/patch values. That can implement IComparable<Version> in an obvious way. Parse all the strings to create a List<Version>, then you can find the maximum value within the list (or sort the whole list). You could even just use the System.Version class if you want, although that supports four parts when it looks like you only want three. If you don't mind it supporting more parts than you need, it would definitely be simpler than writing your own.

List<String> list = new List<String> { "1.0.0", "1.0.2", "1.1.0" };
List<Version> versions = list.Select(x => Version.Parse(x)).ToList();
Version maxVersion = versions.Max();

If you don't need any other versions, you don't even need the second list - just pass the projection into Max:

List<String> list = new List<String> { "1.0.0", "1.0.2", "1.1.0" };
Version maxVersion = list.Max(x => Version.Parse(x));

Having the values parsed as Version objects makes them much easier to work with than having them as strings. For example, you can trivially filter by major version number, or even "anything later than 1.0.5" or whatever you want to do. In general, it's a good idea to parse strings to a type that represents the data you're working with as early as possible, and only format it again as late as possible for display. This applies to strings, numbers, everything.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1
List<string> lst = new List<string> { "1.0.0", "1.0.2", "1.0.15", "1.1.0" };
var value = lst.Max(x => new Version(x));

Result=1.1.0
sebu
  • 2,824
  • 1
  • 30
  • 45
-2
String test = ""1.0.0", "1.0.2", "1.1.0"";
String[] testArray = test.split(", ");

int max = Integer.MIN_VALUE, maxIndex = 0;

for (int i = 0; i < testArray.length; i++) {
     if (Integer.parseInt(testArray[i]) > max) {
         max = Integer.parseInt(testArray[i]);
         maxIndex = i;
     }
}

Hope this will do the job for you.

Abdul Rahman
  • 1,891
  • 1
  • 9
  • 11
  • 1
    What do you expect parsing "1.0.0" to do? The values simply aren't integers. (Additionally, it looks like you're writing Java rather than C#.) – Jon Skeet May 15 '18 at 06:11