Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List.Sort (Custom sorting...)

Tags:

c#

sorting

I have a List object that includes 3 items: Partial, Full To H, and Full To O.

I'm binding this list to an asp OptionButtonList, and it's sorting it in alphabetical order. However, I want to sort the list like the following:

Full To H, Partial, Full To O.

How can I accomplish this?

like image 528
vinco83 Avatar asked Jun 25 '10 15:06

vinco83


People also ask

Can you create your own custom list for sorting in Excel?

You can also create your own custom list, and use them to sort or fill. For example, if you want to sort or fill by the following lists, you'll need to create a custom list, since there is no natural order. A custom list can correspond to a cell range, or you can enter the list in the Custom Lists dialog box.

What is custom sorting?

When lists of values are displayed in an application page, a user can sort the list by clicking on the column headers. The sort order of the rows will be determined by the sort order of the values in the selected column.

How do you sort a custom list in Python?

Custom Sorting With key= For example with a list of strings, specifying key=len (the built in len() function) sorts the strings by length, from shortest to longest. The sort calls len() for each string to get the list of proxy length values, and then sorts with those proxy values.

How do I create a custom sort in a pivot table?

In a PivotTable, click the small arrow next to Row Labels and Column Labels cells. Click a field in the row or column you want to sort. on Row Labels or Column Labels, and then click the sort option you want. To sort data in ascending or descending order, click Sort A to Z or Sort Z to A.


1 Answers

Linq is great for this. You could even build the order sequence up to have it defined on the fly since the execution of the sort is not executed until the ToList.

 var sortedList = yourList.OrderBy(i => i.FullToH).
     ThenBy(i => i.Partial).
     ThenBy(i => i.FullToO).ToList();
like image 148
Kelsey Avatar answered Sep 28 '22 07:09

Kelsey