Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Get the Nth Minimum or Nth Maximum Number from a Collection C#

, 21 Apr 2014
Rate this:
Please Sign up or sign in to vote.
Get the Nth Minimum or Nth Maximum Number from a Collection C#

Introduction

For a collection of integers/decimals, we can use the .Max() and .Min() to get the maximum and minimum value of the collection respectively.

However, there are two more case scenarios that I have faced, which I am going to discuss next. Following is the definition of a class Item that I am going to use here:

class Item: IComparable<Item>
{
    public string itemName { get; set; }
    public int itemPrice { get; set; }

    int IComparable<item>.CompareTo(Item other)
    {
        if (other.itemPrice > this.itemPrice)
            return -1;
        else if (other.itemPrice == this.itemPrice)
            return 0;
        else 
            return 1;
    }
 
//Operator overloading 

        public static bool operator >(Item X, Item Y)
        {
            if (X == null && Y == null)
            {
                return false;
            }
            if (X == null)
            {
                return false; //true?
            }
            if (Y == null)
            {
                return true; //false? 
            }

            return X.itemPrice > Y.itemPrice;
        }

        public static bool operator <(Item X, Item Y)
        {
            if (X == null && Y == null)
            {
                return false;
            }
            if (X == null)
            {
                return true; //false?
            }
            if (Y == null)
            {
                return false; //true? 
            }

            return X.itemPrice < Y.itemPrice;
        } 
 }

Populate some value to the collection:

Item[] items = { new Item { itemName="Item1", itemPrice=10 },
                 new Item { itemName="Item2", itemPrice=20 },
                 new Item { itemName="Item3", itemPrice=30 }, 
                 new Item { itemName="Item4", itemPrice=40 } };

Get the Minimum number greater than a certain value OR Maximum number lesser than a certain value:

//get the minimum valued item = Item1
int min = items.Min().itemPrice;

//get the min valued item greater than the 2nd item = Item3
int min = items.Where(p => p > items[1]).DefaultIfEmpty().Min().itemPrice);

//get the maximum valued item = Item4
int min = items.Max().itemPrice;

//get the max valued item less than the 3rd item = Item2
int min = items.Where(p => p < items[2]).DefaultIfEmpty().Max().itemPrice);

Similarly, we can also get the element at each index:

Item secondLowest = numbers.OrderBy(num => num).ElementAt(1);
Item secondHighest = numbers.OrderBy(num => num).Reverse().ElementAt(1);

CodeProject

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

piyush_singh
Software Developer
India India
No Biography provided
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
GeneralMy vote of 3 Pinprofessionalmcooper198422-Apr-14 4:21 
GeneralRe: My vote of 3 Pinmemberpiyush_singh24-Apr-14 0:16 
QuestionAnother Suggestion. PinmemberGeorge Swan21-Apr-14 3:58 
AnswerRe: Another Suggestion. Pinmemberpiyush_singh21-Apr-14 20:02 
GeneralRe: Another Suggestion. PinmemberGeorge Swan21-Apr-14 22:43 
GeneralRe: Another Suggestion. Pinmemberpiyush_singh24-Apr-14 0:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140926.1 | Last Updated 21 Apr 2014
Article Copyright 2014 by piyush_singh
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid