Store name + number and sort in size order based on the numbers

后端 未结 5 1597
梦如初夏
梦如初夏 2021-01-24 07:20

Doing a college project and I\'m a bit stuck..

Basically, I need to take the string input for an employee name and an integer input for the amount of properties they sol

5条回答
  •  无人及你
    2021-01-24 08:14

    Ideally you would be using a class for these, as seen in the example below.

     public class Owner
    {
        public string Name { get; }
        public int PropertiesSold { get; }
        public Owner(string name, int propertiesSold)
        {
            Name = name;
            PropertiesSold = propertiesSold;
        }
    }
    

    This way you can keep track of the name and # of properties at the same time. Your loop turns into this:

     List owners = new List();
    
            for (int i = 0; i < 2; i++)
            {
    
                Console.WriteLine("Please enter the employee name: ");
                string name  = Console.ReadLine();
    
                Console.WriteLine("Please enter the number of Properties Sold: ");
                int numProperties = int.Parse(Console.ReadLine());
                owners.Add(new Owner(name, numProperties));
            }
    

    And you can use Linqs OrderByDescending as seen here to sort the list of Owners. LINQ Orderby Descending Query.

提交回复
热议问题