Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting highest available number in a List<string>

Tags:

string

c#

list

linq

I have the following strings in a List<string>

vs0, vs1, vs2, vs3, vs4, vs5, ... vs(n)

In my list, they are not sorted and are random. I want to get the string which has the highest int in it. And then get the number out of that string into a int var.

What is the best and fastest way do this?

like image 263
Saeid Yazdani Avatar asked Dec 16 '22 06:12

Saeid Yazdani


1 Answers

var max = myList.OrderByDescending(v => int.Parse(v.Substring(2))).First();

or if you need the highest int

var max = myList.Select(v => int.Parse(v.Substring(2))).Max();
like image 145
Yuriy Faktorovich Avatar answered Dec 18 '22 20:12

Yuriy Faktorovich