EDIT: I think this is what you want:
If you don't mind sorting 'out of place' and reassigning the list, this should work:
collection = collection.GroupBy(item => item.GetType())
.SelectMany(g => g)
.ToList();
or depending on your needs, something like:
collection = collection.OrderBy(item => item.GetType().FullName)
.ToList();
If it must be in-place, then writing a custom comparer and list.Sort
is probably the best choice.
To group the items by type, you can use GroupBy
:
var groupedItems = collection.GroupBy(item => item.GetType());
This uses deferred execution.
Alternatively, you can put the 'groups' into a data-structure like this:
var itemsByTypeLookUp = collection.ToLookup(item => item.GetType());
foreach(A a in itemsByTypeLookUp[typeof(A)])
{
...
}
If you are only looking for a certain type:
var itemsOfTypeA = collection.OfType<A>();