It looks like you already get messed your code design and came to the point where you lost the ability to sort items directly as
IList
does not support this operation. You should do one of the two: either review your design and rethink collection types you use (which is also better) or add a local fix in the created design (which may or may not be acceptable because ultimately you risk having more hacks than codes, which happens all the time). You could also combine the two approaches.
First of all, your should use generic versions of collection interfaces and classes. Use
System.Collections.Generic.IList<T>
instead,
http://msdn.microsoft.com/en-us/library/5y536ey6.aspx[
^]. Non-generic types are rendered obsolete as earlier as of .NET Framework v.2.0 when generics were introduced, so there is absolutely no need to use non-generic non-specialized collection types in new development.
If you agree to do the trick instead of big re-design (which I insist is better), the idea is: you should create a temporary collection, populate if from your instance of
IList
, sort it, get appropriate instance of the object supporting
IList
and use it instead of your non-sorted instance of
IList
which you should leave intact. How about it?
Here is one way to do it: populate a new instance of
System.Collections.Generic.SortedList<TKey, TValue>
with
int
key. It will be always sorted, see
http://msdn.microsoft.com/en-us/library/ms132319.aspx[
^]. If you need to control order, use its constructor with
IComparer<TKey>
argument, see the above link. Take its property
Values
which is of the collection type implementing
IList<TValue>
. At this point, you will complete the round trip: you started with the instance of
IList<TValue>
and finally obtained another instance of
IList<TValue>
, this time sorted to your comparison criterion. Use it to populate your tree node with children. Problem solved.
—SA