- Home /
Question by
sona.viswam · Jun 12, 2013 at 10:16 AM ·
arraylistarraylistgenerics
How to get smallest element and key from int list
My list is declared as
private List<int> values = new List<int>{5, 2, 4, 10, 6, 7, 8, 9, 1};
How to get smallest element and key from this int list ?
Comment
Best Answer
Answer by Chronos-L · Jun 12, 2013 at 11:46 AM
Take a look at List.Min() or other version of List.Min().
private List<string> alphabets = new List<string>{"5", "2", "3", ...};
//Get Min
string smallestString = alphabets.Min();
//index
int index = alphabets.IndexOf( smallestString );
This is an example for string-list, if you are thinking about using the mathematical-value of the string in finding the smallest "string", then it is slightly more complicated.
Since it is a generic list, it works the same way with List