Click here to Skip to main content
15,888,082 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
I have the following method and I need to sort the iList object that is being passed to it (inside this method). I have tried linq but since it's an interface I'm getting errors.

Thanks in advance

C#
private void AddListToTree(ComponentArt.Web.UI.TreeView treeView, IList list)
        {
    //NEED TO SORT THE LIST HERE
    }
Posted
Updated 28-Nov-11 8:47am
v2
Comments
Sergey Alexandrovich Kryukov 28-Nov-11 13:42pm    
What is "iList"? I guess a list object created on iPad... :-)

Oh, I see... this is IList. Did you ever heard that C# is case-sensitive?
--SA
Mastersev 28-Nov-11 13:45pm    
It's an Interface
Sergey Alexandrovich Kryukov 28-Nov-11 15:09pm    
I see. Did you get it: I suggested you fix the typo in capitalization. Again, do you know C# is case-sensitive?
--SA
Mastersev 28-Nov-11 15:24pm    
Yea it was a typo get over it.
BillWoodruff 28-Nov-11 19:59pm    
It would be useful in questions like this if you specified what type of sort you want. And, please give a little more information when something "is" an Interface (as IList is), as, at least to me, that's ambiguous ... is the instance of IList passed in as a parameter a collection of objects that implement an Interface ?

best, Bill

 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 28-Nov-11 15:08pm    
This is a good point, my 5.
--SA
Abhinav S 29-Nov-11 0:39am    
Thank you SA.
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
 
Share this answer
 
Comments
Sander Rossel 28-Nov-11 14:14pm    
My 5. Great answer. To often have I seen (and unfortunately did it myself too) quick and dirty fixes because the design was flawed (if at all present).
Using IList.Cast<Object>().OrderBy(o => o); might work to get an IOrderedEnumerable<Object>, but only if the IList always has IComparables. Fastest fix I can see, although it doesn't sort the original list (which I think should not be done at all in a Method called AddListToTree, it's not called SortListAndAddItToTree, is it?).
Sergey Alexandrovich Kryukov 28-Nov-11 15:00pm    
Thank you, Naerling.
The problem with the cast is the common problem of the cast: we don't know what is the exact class implementing IList. It will cause more trouble in support, too, when a developer decides to replace implementing class.
--SA
Sander Rossel 28-Nov-11 15:08pm    
That is one problem when using Cast, yes :)
And judging from the posts below I am afraid your answer was of no use to the OP. OP does not seems to understand the basics of types and generics (let alone design)...
Mastersev 28-Nov-11 14:23pm    
OK I get the type as following:

Type[] listTypes = list.GetType().GetGenericArguments();
Type listType = null;

if (listTypes.Length > 0)
{
listType = listTypes[0];
}
but how can I create a new list with this type?
Sergey Alexandrovich Kryukov 28-Nov-11 15:07pm    
Why?!! Where did I tell you to do anything with generic arguments, etc?
My solution is straightforward, I've put all types to use step be step....
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900