UWP/C#: ObservableCollection sort in-place (w/o scrolling)

Viewed 1305

In an UWP app I'm trying to sort an ObservableCollection that is bound to a ListView - therefore collection.OrderBy(..) (which creates a new collection) is not an option.

Until now I used this extension-method:

public static void Sort<TSource, TKey>(this 
ObservableCollection<TSource> source, Func<TSource, TKey> keySelector)
{
    List<TSource> sortedList = source.OrderBy(keySelector).ToList();
    source.Clear();
    foreach (var sortedItem in sortedList)
    {
        source.Add(sortedItem);
    }
}

Unfortunately this way the current 'scrolling-offset' is resetted due to source.Clear() and the corresponding ListView scrolls all the way back to the top - which is pretty bad user-experience.

Any ideas?

2 Answers
Related