Have you ever wanted to sort an ICollection? It took me awhile to figure this one out so I thought I should blog it.
I originally posted this quite awhile ago. Since then I have discovered a much easier way to sort a collection.
Here's an update on sorting collections using LINQ. Much simpler:
And here is my original post on the subject. You decide which one looks easier.
The above function retrieves an ICollection puts it into a List object and calls the sort method using an IComparer Object which is a child object of . defined as follows:
I originally posted this quite awhile ago. Since then I have discovered a much easier way to sort a collection.
Here's an update on sorting collections using LINQ. Much simpler:
var orderedList = customer.Users .OrderBy(x => x.UserType) .ToList();
And here is my original post on the subject. You decide which one looks easier.
private IList<EventReqFormSection> GetSections()
{
ICollection<EventReqFormSection> sections = EventReqFormSection.GetByEventReqFormId(_businessObject.Id, ObjectManager);
// sort by Display Sequence
List<EventReqFormSection> sortedValues = new List<EventReqFormSection>(sections);
sortedValues.Sort(new EventReqFormSection.DisplaySequenceSort());
return sortedValues;
}
The above function retrieves an ICollection puts it into a List object and calls the sort method using an IComparer Object which is a child object of . defined as follows:
public class DisplaySequenceSort : IComparer<EventReqFormSection>
{
public int Compare(EventReqFormSection x, EventReqFormSection y)
{
if (x == null)
throw new ArgumentNullException("x");
if (y == null)
throw new ArgumentNullException("y");
return x.DisplaySequence.CompareTo(y.DisplaySequence);
}
}
OMG... Sooo much easier these days with LINQ.
ReplyDeleteCool and that i have a keen give: House Renovation What To Do First home remodel cost
ReplyDelete