StoimenK

C#_Objects_and_Classes_1.3.Articles_2_0

Feb 25th, 2020
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.71 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Object_and_Clasess
  8. {
  9.     class Program
  10.     {
  11.         static void Main()
  12.         {
  13.             int count = int.Parse(Console.ReadLine());
  14.             List<Article> list = new List<Article>();
  15.  
  16.             for (int i = 0; i < count; i++)
  17.             {
  18.                 string[] tokens = Console.ReadLine().Split(',');
  19.  
  20.                 Article myArticle = new Article(tokens[0], tokens[1], tokens[2]);
  21.                 list.Add(myArticle);
  22.             }
  23.  
  24.             string criteria = Console.ReadLine();
  25.             List<Article> sortedArticles = new List<Article>();
  26.  
  27.             if (criteria == "title")
  28.             {
  29.                 sortedArticles = list.OrderBy(a => a.Title).ToList();
  30.             }
  31.             else if (criteria == "content")
  32.             {
  33.                 sortedArticles = list.OrderBy(a => a.Content).ToList();
  34.             }
  35.             else
  36.             {
  37.                 sortedArticles = list.OrderBy(a => a.Author).ToList();
  38.             }
  39.  
  40.             sortedArticles.ForEach(x => Console.WriteLine(x));
  41.          }
  42.  
  43.         class Article
  44.         {
  45.             public string Title { get; set; }
  46.             public string Content { get; set; }
  47.             public string Author { get; set; }
  48.  
  49.             public Article(string title, string content, string author)
  50.             {
  51.                 Title = title;
  52.                 Content = content;
  53.                 Author = author;
  54.             }
  55.  
  56.             public override string ToString()
  57.             {
  58.                 return $"{Title} -{Content}:{Author}";
  59.             }
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment