Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 15.ObjectsAndClasses-Order by Age
- You will receive an unknown number of lines. Each line will be consisted of an array of 3 elements. The first element will be a string and it will represent the name of a person. The second element will be a string and it will represent the ID of the person. The last element will be an integer - the age of the person. When you receive the command “End”, print all the people, ordered by age.
- Examples
- Input Output
- Georgi 123456 20 Stefan with ID: 524244 is 10 years old.
- Pesho 78911 15 Pesho with ID: 78911 is 15 years old.
- Stefan 524244 10 Georgi with ID: 123456 is 20 years old.
- End
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Order_By_Age
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<string> command = Console.ReadLine().Split().ToList();
- List<dynamic> list = new List<dynamic>();//Създаване на обект "dynamic" без да нужно създаването на "class"!
- while (command[0] != "End")
- {
- string name = command[0].ToString();
- string id = command[1].ToString();
- int age = int.Parse(command[2]);
- dynamic person = new { name, id, age };//Така се избягва стандартния начин на създаване на "class"( с пропъртита)!
- list.Add(person);
- command = Console.ReadLine().Split().ToList();
- }
- if (command[0] == "End")
- {
- var result = list.OrderBy(person => person.age);
- foreach (var item in result)
- {
- Console.WriteLine($"{item.name} with ID: {item.id} is {item.age} years old.");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment