Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Hello World!");
- var cats = new List<Cat>
- {
- new Cat{Name = "Mr Muffins", Weight=10, Age = 4},
- new Cat { Name = "Mr Bigglesworth", Weight = 15, Age = 6 }
- };
- //Sort by name
- cats.Sort(new CatNameComparer());
- //Sort by age
- cats.Sort(new CatAgeComparer());
- }
- }
- class Cat
- {
- public string Name{ get; set; }
- public double Weight { get; set; }
- public int Age { get; set; }
- }
- class CatNameComparer : IComparer<Cat>
- {
- public int Compare([AllowNull] Cat x, [AllowNull] Cat y)
- {
- return new CaseInsensitiveComparer().Compare(x.Name, y.Name);
- }
- }
- class CatAgeComparer : IComparer<Cat>
- {
- public int Compare([AllowNull] Cat x, [AllowNull] Cat y)
- {
- if (x.Age > y.Age)
- return 1;
- if (x.Age < y.Age)
- return -1;
- else
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment