There is a list

List<string> list = new List<string>(); list.Add("10;abc"); list.Add("14;hgg"); list.Add("15;xxc"); 

How to find the maximum element of the list array by the value up to; ?

 var rez = list.Select(x=>x.Split(';')[0]).Max(); 

So finds the maximum element only without the second element.

    2 answers 2

    The easiest way to use the package MoreLinq :

     using MoreLinq; 
     list.MaxBy(x => int.Parse(x.Split(';')[0])) 

    It is possible without extraneous packages, a little less efficiently, through sorting:

     list.OrderByDescending(x => int.Parse(x.Split(';')[0])).First(); 
    • And without packages? . - Radzhab
    • @Radzhab: Complete the answer. - VladD

    I do not know how to use C #, but if you know the length of the numbers before ";" , then on java it is like this:

     String a; int max = Integer.MIN_VALUE; int number; int save; for(int i = 0; i < list.getSize(); i++ ){ a = list.get(i); number = Integer.parseInt(a.substring(0, кол-во символов до ";")) if(number > max){ max = number; save = i; } } list.get(save); 

    True, it seems to me so long enough)