I would like to share my thoughts as well, since I've bumped into the same issue.
Well, just answering the question would be:
1 - Add an extenssion to the observable collection class like this:
namespace YourNameSpace
{
public static class ObservableCollectionExtension
{
public static void OrderByReference<T>(this ObservableCollection<T> collection, List<T> comparison)
{
for (int i = 0; i < comparison.Count; i++)
{
if (!comparison.ElementAt(i).Equals(collection.ElementAt(i)))
collection.Move(collection.IndexOf(comparison[i]), i);
}
}
public static void InsertInPlace<T>(this ObservableCollection<T> collection, List<T> comparison, T item)
{
int index = comparison.IndexOf(item);
comparison.RemoveAt(index);
collection.OrderByReference(comparison);
collection.Insert(index, item);
}
}
}
2 - Then use it like this:
_animals.OrderByReference(_animals.OrderBy(x => x).ToList());
This changes your ObservableCollection, you can use linq and it doesn't change the bindings!
Extra:
I've extended @Marco and @Contango answers to my own liking. First I thought of using a list directly as the comparison, so you would have this:
public static void OrderByReference<T>(this ObservableCollection<T> collection, List<T> comparison)
{
for (int i = 0; i < comparison.Count; i++)
{
collection.Move(collection.IndexOf(comparison[i]), i);
}
}
And using like this:
YourObservableCollection.OrderByReference(YourObservableCollection.DoYourLinqOrdering().ToList());
Then I've thought, since this always move everything and triggers the move in the ObservableCollection why not compare if the object is already in there, and this brings what I've put in the begining with the Equals comparator.
Adding the object to the correct place also sounded good, but I wanned a simple way to do it. So I've came up with that:
public static void InsertInPlace<T>(this ObservableCollection<T> collection, List<T> comparison, T item)
{
collection.Insert(comparison.IndexOf(item), item);
}
You send a list with the new object where you want and also this new object, so you need to create a list, then add this new object, like this:
var YourList = YourObservableCollection.ToList();
var YourObject = new YourClass { ..... };
YourList.Add(YourObject);
YourObservableCollection.InsertInPlace(YourList.DoYourLinqOrdering().ToList(), YourObject);
But since the ObservableCollection could be in a different order than the list because of the selection in the "DoYourLinqOrdering()" (this would happen if the collection wasn't previously ordered) I've added the first extession (OrderByReference) in the insert as you can see in the begining of the answer. It will not take long if it doesn't need to move the itens arround, so I did't saw a problem in using it.
As performance goes, I've compared the methods by checking the time it takes for each to finish, so not ideal, but anyway, I've tested an observable collection with 20000 itens. For the OrderByReference I didn't saw great difference in the performance by adding the Equal object checker, but if not all itens need to be moved it is faster and it doesn't fire unecessary Move events on the collecitonChanged, so thats something. For the InsertInPlace is the same thing, if the ObservableCollection is already sorted, just checking if the objects are in the right place is faster than moving all the itens around, so there was not a huge difference in time if it is just passing through the Equals statement and you get the benefit of being sure everything is where it should be.
Be aware that if you use this extession with objects that dont mach or with a list that have more or less objects you will get an ArgumentOutOfRangeException or some other unexpect behaviour.
Hopes this helps somebody!