Get the Name of the Maximum age in 2 lists in C#

I am relatively new to C#.

I am trying to find the name of the maximum age from a list of names and ages. I don't know which names or which ages are given in advance unlike the example below.

The names and ages are created from two lists below.

List<string> name = new List<string>();
List<double> age = new List<double>();

I know how to find the maximum age with the Max() method but how do I get the corresponding name? With the example below is there a way to create new objects from a list?

/// This class implements IComparable to be able to  
/// compare one Pet to another Pet. 
/// </summary> 
class Pet : IComparable<Pet>
{
    public string Name { get; set; }
    public int Age { get; set; }

    /// <summary> 
    /// Compares this Pet to another Pet by  
    /// summing each Pet's age and name length. 
    /// </summary> 
    /// <param name="other">The Pet to compare this Pet to.</param>
    /// <returns>-1 if this Pet is 'less' than the other Pet,  
    /// 0 if they are equal, 
    /// or 1 if this Pet is 'greater' than the other Pet.</returns> 
    int IComparable<Pet>.CompareTo(Pet other)
    {
        int sumOther = other.Age + other.Name.Length;
        int sumThis = this.Age + this.Name.Length;

        if (sumOther > sumThis)
            return -1;
        else if (sumOther == sumThis)
            return 0;
        else 
            return 1;
    }
}

public static void MaxEx3()
{
    Pet[] pets = { new Pet { Name="Barley", Age=8 },
                   new Pet { Name="Boots", Age=4 },
                   new Pet { Name="Whiskers", Age=1 } };

    Pet max = pets.Max();

    Console.WriteLine(
        "The 'maximum' animal is {0}.",
        max.Name);
}

/*
 This code produces the following output:

 The 'maximum' animal is Barley.
*/

Answers


Another solution could be modify the CompareTo() method with:

int IComparable<Pet>.CompareTo(Pet pet)
{
    return this.Age.CompareTo( pet.Age );
}

Then:

var olderPet = pets.Max(); 
Console.WriteLine(olderPet.Name);   

EDIT 1:

How do I get the Name and Age from two lists though? List name = new List(); List age = new List(); is where I have the info.

If I understood you need to create another list of Pets from two list given. So you have to add this to Pet class:

public Pet(string Name, int Age){
    this.Name = Name;
    this.Age = Age;
}

And then do this to create the list of Pets from the two lists:

List<Pet> listPets = new List<Pet>();
for(int i = 0; i < listNames.Count; i++){
    listPets.Add(new Pet(name[i], age[i]));
}

Remember that the two lists must have the same length else this code will throw an ArgumentOutOfRangeException.

EDIT 2:

However when I put it all together I get output of "Pet.Pet". How can I paste the code to show?

If you want to print all the content of the new list you can do:

foreach(Pet pet in listPets)
  Console.WriteLine("Name: " + pet.Name + "," + "Age: " + pet.Age.ToString());

Or you can add this to your pet class:

public override string ToString(){
   return "Name: " + Name + "," + "Age: " + Age.ToString();
}

And then use this code to print all the content of the list:

foreach(Pet pet in listPets)
  Console.WriteLine(pet.ToString());

var pet = pets.OrderByDescending(p => p.Age).First();    

var name = pets.OrderByDescending(p => p.Age).First().Name;

you can also use morelinq's MaxBy

var name = pets.MaxBy(p=>p.Age).Name;

Need Your Help

Delete specific rows from an Access table using Jackcess

java database rcp jackcess

I am using the Jackcess API with an Access database. I open the database and get a specific table. How can I get the data (rows) from this table which matches a list of ids?

Create a transparent HTML cursor

javascript html cursor

Is it possible to create a transparent HTML cursor on a web page? I'm using a div as a cursor, and I want to make the div 50% transparent: http://jsfiddle.net/mCgmP/16/

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.