Dave
Dave

Reputation: 253

Find the item with the lowest value of a property within a list

Let's say I have this class:

class Person {
   public int ID;
   public string Name;
}

And then I have a list of Person's.

List<Person> persons = new List<Person>();

Which is filled with alot of random persons. How do I query the list to get the person with the lowest ID? The objects in the list is in a random order so the person with the lowest ID might not be the very first element. Can I achieve this without sorting the list first?

Upvotes: 8

Views: 10839

Answers (4)

Bhupiister singh
Bhupiister singh

Reputation: 119

List<AnswerInfo> answerinfo;

Public void SampleFunction()
{
        answerinfo = new List<AnswerInfo>();
        //custom hash table storing elapsed time for all users
        float LocalScoreTime = (float.Parse)((string)local.CustomProperties["elapsedTime"]);

        AnswerInfo objc = new AnswerInfo();

        if (CorrectAnswer)
        {
            foreach (PhotonPlayer _player in PhotonNetwork.otherPlayers)
            {
                objc.ID = _player.ID;
                objc.AnsCorrect = (bool)_player.CustomProperties["RemoteAnswer"];
                objc.AnsTime = (float.Parse)((string)_player.CustomProperties["elapsedTime"]);

                answerinfo.Add(objc);
            }
            //This can work too and can be used in future
            //var minID = answerinfo.Min(x => x.AnsTime);
            //var person = answerinfo.First(x => x.AnsTime == minID);

            AnswerInfo minTimePerson = answerinfo[0];
            float minTime = 30;
            foreach (AnswerInfo user in answerinfo)
            {
                if (user.AnsCorrect)
                {
                    if (user.AnsTime < minTime)
                    {
                        minTime = user.AnsTime;
                        minTimePerson = user;
                    }
                }
            }
            Debug.LogFormat("Remote User ID with correct Answer: {0} and lowest time {1}",minTimePerson.ID,minTimePerson.AnsTime);
            if(LocalScoreTime < minTimePerson.AnsTime)
            {
                local.AddScore(1);
                localplayerscore_textfield.color = Color.green;
            }
        }
}
[Serializable]
public class AnswerInfo
{
    public bool  AnsCorrect;
    public float AnsTime;
    public int ID;
}

Upvotes: 0

Hamid Pourjam
Hamid Pourjam

Reputation: 20764

this is without sorting the list and just iterates the list once.

Person minIdPerson = persons[0];
foreach (var person in persons)
{
    if (person.ID < minIdPerson.ID)
        minIdPerson = person;
}

Upvotes: 9

galenus
galenus

Reputation: 2137

Use the Min extension method of LINQ:

persons.Min(p => p.ID)

EDIT:

My bad, the previous method returns only the lowest ID, so in case you'd like to use only built-in LINQ methods, here you go:

persons.Aggregate(
    (personWithMinID, currentPerson) =>
        currentPerson.ID <= personWithMinID.ID ? currentPerson : personWithMinID)

Upvotes: 5

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101742

You can use MinBy method from More Linq library:

var person = persons.MinBy(x => x.ID);

If you can't use a third party library you can get the min ID first and then get the person that has the min ID:

var minID = person.Min(x => x.ID);
var person = persons.First(x => x.ID == minID);

Upvotes: 9

Related Questions