123
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SortedSetGenerics { class Program { static void Main(string[] args) { UseSortedSet(); Console.ReadLine(); } static void UseSortedSet() { SortedSet<Person> peopleSortedSet = new SortedSet<Person>(new SortpeopleByAge()) { new Person { Age = 123, FirstName = "123", LastName = "123" }, new Person { Age = 1234, FirstName = "1234", LastName = "1234" }, new Person { Age = 12345, FirstName = "12345", LastName = "12345" }, }; foreach (Person p in peopleSortedSet) { Console.WriteLine(p); // elements sorted by age } } public class Person { public int Age { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public Person() { } public Person(int age, string firstName, string lastName) { Age = age; FirstName = firstName; LastName = lastName; } public override string ToString() { return ("Age="+Age); // return base.ToString(); } } class SortpeopleByAge : IComparer<Person> { public int Compare(Person x, Person y) { if (x.Age > y.Age) { return 1; } if (x.Age > y.Age) { return -1; } else return 0; //throw new NotImplementedException(); } } } } |