using System; using System.Diagnostics; using System.Collections.Generic; namespace EggHeadCafe { class EntryPoint { [STAThread] static void Main(string[] args) { EntryPoint tester = new EntryPoint(); try { tester.Test1(); } catch (Exception e) { Debug.WriteLine(e.Message); } finally { Console.ReadLine(); } } private void Test1() { Section section = null; SectionChild sectionChild = null; List<Section> sections = null; List<SectionChild> sectionChildren = null; int testRecordCount = 10; try { sections = new List<Section>(); sectionChildren = new List<SectionChild>(); // Add some test data for (int i = testRecordCount; i > -1; i--) { section = new Section(); section.SectionID = i; section.SortOrder = i; section.Description = "Section " + i.ToString(); section.SectionSubName = "My Sub Name " + i.ToString(); sections.Add(section); } for (int i = testRecordCount; i > -1; i--) { sectionChild = new SectionChild(); sectionChild.GridSectionID = i.ToString(); sectionChild.SortOrder = i; sectionChild.Description = "SectionChild " + i.ToString(); sectionChildren.Add(sectionChild); } Console.WriteLine(""); Console.WriteLine("Demonstrate sorting Section records"); Console.WriteLine(""); DemonstrateSort(sections); Console.WriteLine(""); Console.WriteLine("Demonstrate sorting Section SubName Sort"); Console.WriteLine(""); sections.Sort(SectionComparer.CompareSectionSubName); for (int i = 0; i < sections.Count; i++) { Console.WriteLine(sections[i].SectionSubName); } Console.WriteLine(""); sections.Reverse(); for (int i = 0; i < sections.Count; i++) { Console.WriteLine(sections[i].SectionSubName); } Console.WriteLine(""); Console.WriteLine("Demonstrate sorting SectionChild records"); Console.WriteLine(""); DemonstrateSort(sectionChildren); } catch (Exception) { throw; } } private void DemonstrateSort<T>(List<T> list) where T : StandardProperties { try { Console.WriteLine(""); Console.WriteLine("Sorted by SortOrder ASC"); Console.WriteLine(""); // Use our static comparer for the SortOrder Property list.Sort(StandardPropertiesComparer.CompareSortOrder); WriteToConsoleScreen(list); Console.WriteLine(""); Console.WriteLine("Sorted by SortOrder DESC"); Console.WriteLine(""); list.Reverse(); WriteToConsoleScreen(list); Console.WriteLine(""); Console.WriteLine("Sorted by SortOrder ASC"); Console.WriteLine(""); list.Sort(StandardPropertiesComparer.CompareSortOrder); WriteToConsoleScreen(list); Console.WriteLine(""); Console.WriteLine("Sorted by Description ASC"); Console.WriteLine(""); // Let's try sorting strings like the Description property // with our static CompareDescription comparer. list.Sort(StandardPropertiesComparer.CompareDescription); WriteToConsoleScreen(list); } catch (Exception) { throw; } } private void WriteToConsoleScreen<T>(List<T> list) where T : StandardProperties { try { Console.WriteLine(" "); for (int i = 0; i < list.Count; i++) { Console.WriteLine(list[i].Description); } } catch (Exception) { throw; } } #endregion } // Create a sample Section class that inherits StandardProperties public class Section : StandardProperties { public int SectionID = 0; public int BlahForeignKeyID = 0; public int OtherBlahForeignKeyID = 0; public string SectionSubName = ""; } // Create a sample SectionChild class that inherits StandardProperties public class SectionChild : StandardProperties { public string GridSectionID = ""; public int SomeForeignKeyID = 0; public int SomeOtherForeignKeyID = 0; } // This class holds our standard properties and standard comparison // methods required by the IComparable interface. public class StandardProperties : IComparable<StandardProperties> { private bool columnInfoIsDirty = false; public bool IsDirty { get { return columnInfoIsDirty; } set { columnInfoIsDirty = value; } } private bool columnInfoIsFlaggedForDelete = false; public bool IsFlaggedForDelete { get { return columnInfoIsFlaggedForDelete; } set { if (columnInfoIsFlaggedForDelete != value) { IsDirty = true; columnInfoIsFlaggedForDelete = value; } } } private int columnInfoSortOrder = 0; public int SortOrder { get { return columnInfoSortOrder; } set { if (columnInfoSortOrder != value) { IsDirty = true; columnInfoSortOrder = value; } } } private string columnInfoDescription = ""; public string Description { get { return columnInfoDescription; } set { if (columnInfoDescription != value) { IsDirty = true; columnInfoDescription = value; } } } // Create overloads for Equals to support // our various needs for comparison by SortOrder // or Description properties. public bool Equals(StandardProperties comparedTo) { if (this.SortOrder == comparedTo.SortOrder) { return true; } return false; } public bool Equals(string comparedTo) { if (this.Description == comparedTo) { return true; } return false; } // Create overloads to support comparison methods // sorting on the SortOrder property or by the // a couple of strings. public int CompareTo(StandardProperties comparedTo) { return this.SortOrder.CompareTo(comparedTo.SortOrder); } public int CompareTo(string thisItem,string comparedTo) { return String.Compare(thisItem,comparedTo); } } // Create a static instance for our Standard Comparer. We'll // be passing these as parameters to the .Sort method of our List. public class StandardPropertiesComparer { public static int CompareSortOrder(StandardProperties thisItem, StandardProperties comparedTo) { return thisItem.CompareTo(comparedTo); } public static int CompareDescription(StandardProperties thisItem, StandardProperties comparedTo) { return thisItem.CompareTo(thisItem.Description, comparedTo.Description); } } // This is a custom class for demonstration purposes. // I wanted to show how you could create your own comparer // class where you could incorporate one or more comparison // methods. public class SectionComparer : StandardPropertiesComparer { public static int CompareSectionSubName(Section thisItem, Section comparedTo) { return String.Compare(thisItem.SectionSubName,comparedTo.SectionSubName); } } }