Find object with minimum or maximum property value in C#
This post will discuss how to find an object with a minimum or maximum property value in C#.
1. Using MinBy() and MaxBy() method
You can get an object with minimum and maximum value of a specific property in linear time using LINQ’s MinBy() and MaxBy() methods. The MinBy() and MaxBy() methods return the minimum and the maximum value in a sequence, respectively, according to the provided key selector function and key comparer. The following example provides a simple illustration:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
using System; using System.Linq; public class Person { public string name; public int age; public Person(string name, int age) { this.name = name; this.age = age; } public override string ToString() { return "[" + name + ", " + age + "]"; } } public class Example { public static void Main() { Person[] people = { new Person("x", 27), new Person("y", 21), new Person("z", 24), new Person("z", 30) }; var maxAge = people.MaxBy(x => x.age); var minAge = people.MinBy(x => x.age); // Minimum Age=[y, 21], Maximum Age=[z, 30] Console.WriteLine($"Minimum Age={minAge}, Maximum Age={maxAge}"); } } |
Note that both MinBy() and MaxBy() methods are included with LINQ and requires System.Linq namespace. If you don’t want to use LINQ, you can find the object with minimum/maximum value by traversing the sequence using a for-loop, and comparing each encountered item with the minimum/maximum value found so far.
2. Using Aggregate() method
LINQ’s Aggregate() function is used to apply an accumulator function to each element of a sequence. You can use it as follows to find a Person object with minimum and maximum Age in an array/list:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
using System; using System.Linq; public class Person { public string name; public int age; public Person(string name, int age) { this.name = name; this.age = age; } public override string ToString() { return "[" + name + ", " + age + "]"; } } public class Example { public static void Main() { Person[] people = { new Person("x", 27), new Person("y", 21), new Person("z", 24), new Person("z", 30) }; var maxAge = people.Aggregate((max, next) => next.age > max.age ? next : max); var minAge = people.Aggregate((min, next) => next.age < min.age ? next : min); // Minimum Age=[y, 21], Maximum Age=[z, 30] Console.WriteLine($"Minimum Age={minAge}, Maximum Age={maxAge}"); } } |
3. Using Sorting (Not Recommended)
Another approach is to sort the sequence based on the required attribute and select the first item in the sorted sequence as the object with minimum value and the last item in the sorted sequence as the object with maximum value. This approach is not recommended as sorting takes O(n.log(n)) time.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
using System; using System.Linq; public class Person { public string name; public int age; public Person(string name, int age) { this.name = name; this.age = age; } public override string ToString() { return "[" + name + ", " + age + "]"; } } public class Example { public static void Main() { Person[] people = { new Person("x", 27), new Person("y", 21), new Person("z", 24), new Person("z", 30) }; var sorted = people.OrderBy(p => p.age); var minAge = sorted.First(); var maxAge = sorted.Last(); // Minimum Age=[y, 21], Maximum Age=[z, 30] Console.WriteLine($"Minimum Age={minAge}, Maximum Age={maxAge}"); } } |
That’s all about finding the object with minimum or maximum property value in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)