Advertisement
Morogn93

Interfejsy sortowanie

Mar 8th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.38 KB | None | 0 0
  1.  class Program
  2.     {
  3.         static void Main(string[] args)
  4.         {
  5.             Console.WriteLine("Hello World!");
  6.  
  7.             List<Shoe> shoeCloset = new List<Shoe>();
  8.             shoeCloset.Add(new Shoe() { Size = 19, Style = Style.Clogs, Color = "czarny" });
  9.             shoeCloset.Add(new Shoe() { Size = 18, Style = Style.Sneakers, Color = "zielony" });
  10.             shoeCloset.Add(new Shoe() { Size = 15, Style = Style.Wingtips, Color = "czerwony" });
  11.             shoeCloset.Add(new Shoe() { Size = 114, Style = Style.Clogs, Color = "niebieski" });
  12.             shoeCloset.Add(new Shoe() { Size = 11, Style = Style.Sneakers, Color = "czarny" });
  13.             shoeCloset.Add(new Shoe() { Size = 12, Style = Style.Wingtips, Color = "czerwony" });
  14.  
  15.             int numberOfShoes = shoeCloset.Count;
  16.  
  17.             ComparerBySize comparerShoeBySize = new ComparerBySize();
  18.             shoeCloset.Sort(comparerShoeBySize);
  19.  
  20.             comparerShoeBySize.PrintShoe(shoeCloset);
  21.  
  22.             foreach (Shoe shoe in shoeCloset)
  23.             {
  24.                 Console.WriteLine(shoe.Size.ToString(), shoe.Color, shoe.Style.ToString());
  25.             }
  26.             shoeCloset.Sort();
  27.             foreach (Shoe shoe in shoeCloset)
  28.             {
  29.                 Console.WriteLine(shoe.Size.ToString(), shoe.Color, shoe.Style.ToString());
  30.             }
  31.             Console.ReadLine();
  32.         }
  33.     }
  34.  
  35.     public enum Style
  36.     {
  37.         Sneakers,
  38.         Clogs,
  39.         Wingtips,
  40.         Loafers
  41.     }
  42.  
  43.     public class Shoe : IComparable<Shoe>
  44.     {
  45.         public string Color { get; set; }
  46.         public Style Style { get; set; }
  47.         public int Size;
  48.  
  49.         public int CompareTo(Shoe other)
  50.         {
  51.             if (this.Size > other.Size)
  52.                 return 1;
  53.             else if (this.Size < other.Size)
  54.                 return -1;
  55.             else return 0;
  56.         }
  57.  
  58.     }
  59.     public class ComparerBySize : IComparer<Shoe>
  60.     {
  61.         public int Compare(Shoe x, Shoe y)
  62.         {
  63.             if (x.Size < y.Size) return 1;
  64.             else if (x.Size > y.Size) return -1;
  65.             else return 0;
  66.         }
  67.  
  68.         public void PrintShoe(List<Shoe> shoes)
  69.         {
  70.             foreach (Shoe item in shoes)
  71.             {
  72.                 Console.WriteLine(item.Size.ToString() + "-centymetorwy but" + item.Style.ToString());
  73.             }
  74.         }
  75.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement