Don't miss out Virtual Happy Hour today (April 26).

Try our conversational search powered by Generative AI!

Sort PageTypeCollection?

Vote:
 
Hi, I want to have a list of all pagetypes sorted alphabetically. The list is no problems using EPiServer.DataAbstraction.PageType.List(), but I can´t find an easy way to do the sorting. Any ideas? Regards Sandra Larsson
#12478
Dec 21, 2005 10:46
Vote:
 
This code will do the sorting. PageTypeCollection ptc = PageType.List(); foreach(PageType pt in ptc) { System.Diagnostics.Debug.WriteLine(pt.LocalizedName); } ptc = SortPageTypes(ptc, "LocalizedName"); foreach(PageType pt in ptc) { System.Diagnostics.Debug.WriteLine(pt.LocalizedName); } internal static PageTypeCollection SortPageTypes(PageTypeCollection unSortedCollection, string sortProperty) { PageTypeCollection sortedCollection = new PageTypeCollection(); foreach(PageType pageType in unSortedCollection) { System.Reflection.PropertyInfo propInfo = pageType.GetType().GetProperty(sortProperty); if(propInfo == null) { throw new ApplicationException(string.Format("Property '{0}' not a member of type '{1}'", sortProperty, pageType.GetType().Name)); } object currentValue = propInfo.GetValue(pageType, null); bool inserted = false; if(currentValue != null) { for(int i = 0; i < sortedCollection.Count; i++) { object compareValue = sortedCollection[i].GetType().GetProperty(sortProperty).GetValue(sortedCollection[i], null); if(currentValue.ToString().CompareTo(compareValue) < 0) { sortedCollection.Insert(i, pageType); inserted = true; break; } } if(!inserted) { sortedCollection.Add(pageType); } } } return sortedCollection; } /HAXEN
#14339
Dec 21, 2005 14:43
Vote:
 
Couldn't you use the SortedList? EPiServer.DataAbstraction.PageTypeCollection ptc = EPiServer.DataAbstraction.PageType.List(); System.Collections.SortedList sl = new System.Collections.SortedList(); foreach (EPiServer.DataAbstraction.PageType pt in ptc) { sl.Add(pt.Name, pt); }
#14340
Dec 21, 2005 15:18
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.