I would like to take a list, re-order it and replace the original.
Is there a better way to do this? Currently I am re-assigning it and it feels silly...
Here is my code:
var viewModel = new ViewModel();
viewModel.Children = viewModel.Children.OrderBy(x => x.Name).ToList();
It seems you are looking for the List<T>.Sort
method.
viewModel.Children.Sort((a, b) => string.Compare(a.Name, b.Name));
Try to use List<T>.Sort
method instead of.
Sorts the elements in the entire
List<T>
using the specifiedSystem.Comparison<T>
.
viewModel.Children.Sort((a, b) => String.Compare(a.Name, b.Name))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With