Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace StrategyPattern
- {
- using System;
- using System.Collections.Generic;
- public class Program
- {
- public static void Main()
- {
- SortedSet<Person> byAge = new SortedSet<Person>(new AgeComp());
- SortedSet<Person> byName = new SortedSet<Person>(new NameComp());
- int numbrOfPeople = int.Parse(Console.ReadLine());
- for (int i = 0; i < numbrOfPeople; i++)
- {
- string[] personDetails = Console.ReadLine().Split();
- var person = new Person(personDetails[0], int.Parse(personDetails[1]));
- byAge.Add(person);
- byName.Add(person);
- }
- Console.WriteLine(string.Join(Environment.NewLine, byName));
- Console.WriteLine(string.Join(Environment.NewLine, byAge));
- }
- }
- public class RandomComp : IComparer<int>
- {
- public int Compare(int x, int y)
- {
- return 0;
- }
- }
- public class NameComp : IComparer<Person>
- {
- public int Compare(Person x, Person y)
- {
- int result = x.name.Length.CompareTo(y.name.Length);
- if (result == 0)
- {
- string fisrtChar = x.name[0].ToString();
- string scondFirstChar = y.name[0].ToString();
- result = String.Compare(fisrtChar, scondFirstChar, false);
- }
- return result;
- }
- }
- public class AgeComp : IComparer<Person>
- {
- public int Compare(Person x, Person y)
- {
- return x.age.CompareTo(y.age);
- }
- }
- public class Person
- {
- public string name;
- public int age;
- public Person(string name, int age)
- {
- this.name = name;
- this.age = age;
- }
- public override string ToString()
- {
- return this.name + " " + this.age;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment