Reputation: 1808
In my program I have TreeView
nodes that I need to be able to shift up and down, basically change the order. My TreeView
is an ObservableCollection
of a specific Data Model. Each node has a property called "Rank", this is the value that I would like to sort the collection by. With that being said I referred to this question. From that question I discovered this blog page. I am attempting the second method involving the sort
function of a List
.
This is the example that I am looking at:
List<Person> list = new List<Person>(people);
list.Sort();
Please note that the "Rank" value of each node is in working order and changing correctly. I just need to find a way to re-order the collection based off of that property and reflect it in the View.
My Problem: When trying to implement the above solution I get an InvalidOperationException
. I feel like I do not understand how to tell the List
to sort based off of rank.
What my code looks like:
List<TreeModel> sortedTree = new List<TreeModel>(TreeCollection);
sortedTree.Sort();
What am I missing here? How do I sort the collection based off of the rank property and reflect those changes in the view?
Thank you.
*I believe I may have posted about this before, so if for some reason this question is too similar my older one, I'll just delete the old one.
Upvotes: 2
Views: 1507
Reputation: 102743
Sort
throws InvalidOperationException
its component type does not have a default comparison:
The default comparer Comparer.Default cannot find an implementation of the IComparable generic interface or the IComparable interface for type T.
You can however supply the comparison as the first parameter to Sort
:
sortedTree.Sort((x, y) => x.Rank.CompareTo(y.Rank));
To pass the sorted items back to the original collection, you could either clear/repopulate CurrentCollection
, or simply assign it a new instance (don't forget to RaisePropertyChanged
if you do the latter):
CurrentCollection = new ObservableCollection<TreeModel>(sortedTree);
Upvotes: 1
Reputation: 81243
You need to pass property name
on which you want to sort your list like this -
sortedTree = sortedTree.OrderBy(m => m.Rank).ToList();
Upvotes: 1