I have a generic collection of type MyImageClass, and MyImageClass has an boolean property "IsProfile". I want to sort this generic list which IsProfile == true stands at the start of the list.
I have tried this.
rptBigImages.DataSource = estate.Images.OrderBy(est=>est.IsProfile).ToList();
with the code above the image stands at the last which IsProfile property is true. But i want it to be at the first index. I need something Asc or Desc. Then i did this.
rptBigImages.DataSource = estate.Images.OrderBy(est=>est.IsProfile).Reverse.ToList();
Is there any easier way to do this ?
Thanks
Use Comparer<T> to sort the SortedList in descending order instead of creating a separate class. Thus, you can create an instance of SortedList<TKey, TValue> to sort the collection in descending order.
Ascending order means the smallest or first or earliest in the order will appear at the top of the list: For numbers or amounts, the sort is smallest to largest. Lower numbers or amounts will be at the top of the list. For letters/words, the sort is alphabetical from A to Z.
Sort() Method Set -1. List<T>. Sort() Method is used to sort the elements or a portion of the elements in the List<T> using either the specified or default IComparer<T> implementation or a provided Comparison<T> delegate to compare list elements.
The reverse() method is used to reverse the order of elements in the entire list (learn more here). In that case, first, use the sort() method that will order the list in ascending order. After that, use the reverse() method for sorting that list in descending order.
How about:
estate.Images.OrderByDescending(est => est.IsProfile).ToList()
This will order the Images in descending order by the IsProfile Property and then create a new List from the result.
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