TheBulgarianWolf

Order By Age

Jan 7th, 2021
872
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.32 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace OrderByAge
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Console.WriteLine("Start entering information in the format Name ID Age: ");
  12.             string input;
  13.             List<Person> people = new List<Person>();
  14.             while((input = Console.ReadLine()) != "end")
  15.             {
  16.                 string[] inputInfo = input.Split(" ");
  17.                 string personName = inputInfo[0];
  18.                 int personID = int.Parse(inputInfo[1]);
  19.                 int personAge = int.Parse(inputInfo[2]);
  20.                 Person person = new Person(personName, personID, personAge);
  21.                 people.Add(person);
  22.             }
  23.  
  24.             var byAge = people.OrderByDescending(x => x.Age);
  25.             foreach (Person person in byAge)
  26.             {
  27.                 Console.WriteLine(person.Name + " with ID " + person.ID + " is " + person.Age + " years old.");
  28.             }
  29.         }
  30.     }
  31.  
  32.     class Person
  33.     {
  34.  
  35.         public Person(string name,int id,int age)
  36.         {
  37.             Name = name;
  38.             ID = id;
  39.             Age = age;
  40.         }
  41.         public string Name { get; set; }
  42.         public int ID { get; set; }
  43.         public int Age { get; set; }
  44.  
  45.     }
  46. }
  47.  
Add Comment
Please, Sign In to add comment