/// /// Comapres the items Parent List and then the Items Name /// Sort by List Name, then Name Field of Item /// public class SortBySPListandSPListItem : IComparer { /// /// Compare /// /// Object to compare against /// Object to compare /// Less than zero, obj1 is less than obj2. Zero, obj1 equals obj2. Greater than zero, obj1 is greater than obj2. public int Compare(object obj1, object obj2) { SPListItem Source = null; SPListItem Dest = null; // Null Checking and casting as SPListItems #region Source if (obj1 != null) { if (obj1.GetType() == typeof(SPListItem)) { // its a list item, TICK! Source = obj1 as SPListItem; } } #endregion #region Destination if (obj2 != null) { if (obj2.GetType() == typeof(SPListItem)) { // its a list item, TICK! Dest = obj2 as SPListItem; } } #endregion // Hand it over to the strongly typed method return this.Compare(Source, Dest); } /// /// Compares two List Items based on List & ListItem["Name"] /// /// The List Item to compare against /// A list item to comapre against item1 /// Less than zero, obj1 is less than obj2. Zero, obj1 equals obj2. Greater than zero, obj1 is greater than obj2. public int Compare(SPListItem item1, SPListItem item2) { // Check for null if ((item1 == null) || (item2 == null)) { return -1; } int CompareResult = 0; // "Outer" Compare.. of the list name int SPListOuterCompareResult = item1.ParentList.Title.ToLower().CompareTo(item2.ParentList.Title.ToLower()); // If we dont equal zero, return out if (SPListOuterCompareResult != 0) { CompareResult = SPListOuterCompareResult; } else { // for anything else, do the inner compare on the listitem int SPListItemInnerCompareResult = item1["Name"].ToString().ToLower().CompareTo(item2["Name"].ToString().ToLower()); CompareResult = SPListItemInnerCompareResult; } // return the result return CompareResult; } }