How to sort iList (with linq or without)

Possible duplicate:
Sort IList in C #

I have the following method, and I need to sort the iList object that is passed to it (inside this method). I tried linq, but since it is an interface, I get errors.

Thank you in advance

private void AddListToTree(ComponentArt.Web.UI.TreeView treeView, IList list) { //NEED TO SORT THE LIST HERE } 

Please note that my type is dynamic.

I think I need to create a temporary collection, fill it out, if from my IList instance, sort it, get the corresponding instance of an object that supports IList, and use it instead of my unsorted IList instance, which I should leave unchanged. So I tried to get the following type:

 Type[] listTypes = list.GetType().GetGenericArguments(); Type listType = null; if (listTypes.Length > 0) { listType = listTypes[0]; } 

But I can not create a new List with this type

+4
source share
6 answers

You must use the generic IList form to be able to use LINQ extension methods:

 private void AddListToTree<T>(ComponentArt.Web.UI.TreeView treeView, IList<T> list) { var orderedList = list.OrderBy(t => t); // ... } 

If you cannot change the signature of the method, but know the type of objects in IList , you can use Cast :

 private void AddListToTree(ComponentArt.Web.UI.TreeView treeView, IList list) { var orderedList = list.Cast<SomeType>().OrderBy(x => x); // ... } 
+9
source

You can use Cast<T>() to change it to IList<T> , then use OrderBy() :

 private void AddListToTree<T>(ComponentArt.Web.UI.TreeView treeView, IList list) { var ordered = list.Cast<T>().OrderBy(e => e); } 

Somewhere you will need to find out the type of sorting.

+1
source

For LINQ: Duplicate the question as: Sort IList in C #

You can also use extension methods to add the Sort command to IList: Why is there no sorting for IList <T>?!?! (Edited by)

You can do the following (WhicH, I do not approve, but it is still possible)

 IList list; ArrayList al = new ArrayList(list); al.Sort(); 

You can also use IComparable: http://devpinoy.org/blogs/keithrull/archive/2007/03/23/sorting-a-generic-list-of-object-in-c-using-icomparable-and-anonymous- delegates.aspx

or

http://foxsys.blogspot.com/2007/06/how-to-sort-generic-ilist.html

+1
source

In one direction (note that you must specify the type of the element in the from clause):

 IList list = new ArrayList(); list.Add("z"); list.Add("a"); list.Add("c"); IEnumerable<string> ordered = from string item in list orderby item select item; foreach (var s in ordered) { Console.WriteLine(s); } 

Print

  a
 c
 z 

Note. Based on your later comment

they can be type 3 of different classes

it will not work. I would seriously think about reorganizing the calling code.

If you cannot edit the types, and if the types should not be mixed in a sorted list, select the option:

 var one = list.OfType<TypeOne>().OrderBy(x => x.Id); var two = list.OfType<TypeTwo>().OrderBy(x => x.Name); var three = list.OfType<TypeThree>().OrderBy(x => x.Nom); var result = one.Cast<object>() .Concat(two.Cast<object>()) .Concat(three.Cast<object>()); 
+1
source

One option is to create an ArrayList adapter around an IList using ArrayList.Adapter , then sort it using ArrayList.Sort . This is if you need an in-place sort operation that mutates the contents of the array, rather than returning a sorted enumeration.

0
source

If you cannot change the signature or use LINQ, use the extension method or your own method.

Examples asked in a duplicate question pointed out by @Ryan Ternier:

LINQ: Duplicate Question: Sort IList in C #

0
source

Source: https://habr.com/ru/post/1383487/


All Articles