Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort Generic List Asc or Desc?

Tags:

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

like image 239
Barbaros Alp Avatar asked Feb 10 '09 11:02

Barbaros Alp


People also ask

How do you sort the generic SortedList in the descending order?

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.

What is ASC sorting?

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.

Can you sort a list C#?

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.

How do you sort and reverse a list in C#?

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.


1 Answers

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.

like image 181
Ray Booysen Avatar answered Dec 22 '22 00:12

Ray Booysen