Get Max value from List<myType>

ListC# 2.0

List Problem Overview


I have List List<MyType>, my type contains Age and RandomID

Now I want to find the maximum age from this list.

What is the simplest and most efficient way?

List Solutions


Solution 1 - List

Assuming you have access to LINQ, and Age is an int (you may also try var maxAge - it is more likely to compile):

int maxAge = myTypes.Max(t => t.Age);

If you also need the RandomID (or the whole object), a quick solution is to use MaxBy from MoreLinq

MyType oldest = myTypes.MaxBy(t => t.Age);

Solution 2 - List

Okay, so if you don't have LINQ, you could hard-code it:

public int FindMaxAge(List<MyType> list)
{
    if (list.Count == 0)
    {
        throw new InvalidOperationException("Empty list");
    }
    int maxAge = int.MinValue;
    foreach (MyType type in list)
    {
        if (type.Age > maxAge)
        {
            maxAge = type.Age;
        }
    }
    return maxAge;
}

Or you could write a more general version, reusable across lots of list types:

public int FindMaxValue<T>(List<T> list, Converter<T, int> projection)
{
    if (list.Count == 0)
    {
        throw new InvalidOperationException("Empty list");
    }
    int maxValue = int.MinValue;
    foreach (T item in list)
    {
        int value = projection(item);
        if (value > maxValue)
        {
            maxValue = value;
        }
    }
    return maxValue;
}

You can use this with:

// C# 2
int maxAge = FindMaxValue(list, delegate(MyType x) { return x.Age; });

// C# 3
int maxAge = FindMaxValue(list, x => x.Age);

Or you could use LINQBridge :)

In each case, you can return the if block with a simple call to Math.Max if you want. For example:

foreach (T item in list)
{
    maxValue = Math.Max(maxValue, projection(item));
}

Solution 3 - List

Solution 4 - List

var maxAge = list.Max(x => x.Age);

Solution 5 - List

thelist.Max(e => e.age);

Solution 6 - List

Easiest way is to use System.Linq as previously described

using System.Linq;

public int GetHighestValue(List<MyTypes> list)
{
    return list.Count > 0 ? list.Max(t => t.Age) : 0; //could also return -1
}

This is also possible with a Dictionary

using System.Linq;

public int GetHighestValue(Dictionary<MyTypes, OtherType> obj)
{
    return obj.Count > 0 ? obj.Max(t => t.Key.Age) : 0; //could also return -1
}

Solution 7 - List

Simplest is actually just Age.Max(), you don't need any more code.

Solution 8 - List

How about this way:

List<int> myList = new List<int>(){1, 2, 3, 4}; //or any other type
myList.Sort();
int greatestValue = myList[ myList.Count - 1 ];

You basically let the Sort() method to do the job for you instead of writing your own method. Unless you don't want to sort your collection.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWaheedView Question on Stackoverflow
Solution 1 - ListKobiView Answer on Stackoverflow
Solution 2 - ListJon SkeetView Answer on Stackoverflow
Solution 3 - ListanishMarokeyView Answer on Stackoverflow
Solution 4 - Listch007View Answer on Stackoverflow
Solution 5 - ListthelostView Answer on Stackoverflow
Solution 6 - ListJason NewlandView Answer on Stackoverflow
Solution 7 - ListEmileView Answer on Stackoverflow
Solution 8 - ListBehnam RasooliView Answer on Stackoverflow