Question: I have an `IList<T>` that I need to sort, and I would rather not copy the list if possible. I've noticed that `ArrayList` has an `Adapter` static method that wraps the passed list without copying it, but this takes an `IList` and I have an `IList<T>`. Is it safe to cast from a `System.Collections.Generic.IList<T>` to a `System.Collections.IList` and just use the `Adapter` method?

Note that this is .Net 2.0, so LINQ is not an option.

Answer: 
You cannot cast IList(T) to IList.

After some sniffing with Reflector, it seems like ArrayList.Adapter(IList).Sort() will first copy the list to an object array, sort the array and then copy the array back to a list:

```
object[] array = new object[count];
this.CopyTo(index, array, 0, count);
Array.Sort(array, 0, count, comparer);
for (int i = 0; i < count; i++)
{
    this._list[i + index] = array[i];
}

```

You might get boxing overhead if T in your List(T) a value type. 

If you need to alter the sequence of the objects in the list that you have, you can do it similarly:

```
IList<object> unsorted = ...
List<object> sorted = new List<object>(unsorted);
sorted.Sort(); 
for (int i = 0; i < unsorted.Countt; i++)
{
    unsorted[i] = sorted[i];
}

```

If the list is so huge (as in hundreds of million items) that you cannot make an extra copy in memory, I suggest using a List(T) in the first place or implement your favorite in-place sorting algorithm.
I know it isn't .NET 2.0 but I love LINQ so much and will endorse it every chance I get :)

**Simple Sort:**

```
var sortedProducts =
    from p in products
    orderby p.ProductName
    select p;

ObjectDumper.Write(sortedProducts);

```

**Sort by multiple conditions:**

```
string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };

var sortedDigits =
    from d in digits 
    orderby d.Length, d
    select d;

```

Both examples are from [101 Linq Samples](http://msdn.microsoft.com/en-us/vcsharp/aa336756.aspx)