Cathal O 'Donnell Cathal O 'Donnell - 3 months ago 12
ASP.NET (C#) Question

MVC - Ordered List

I am trying to order the following list by descending but I can't seem to figure out how it is done:

var cust = item.Profiles.Select(c => new
{
id = c.CustId,
Name = c.Name
}).ToList();
ViewBag.Customers = new MultiSelectList(cust, "id", "Name");


This is what I have already tried:

var cust = item.Profiles.Select(c => new
{
id = c.CustId,
Name = c.Name.OrderByDescending();
}).ToList();
ViewBag.Customers = new MultiSelectList(cust, "id", "Name");


This is how the list is displayed on my view:

@Html.DropDownList("id", (MultiSelectList)ViewBag.Customers, new { @class = "form-control", id = "lstCustomer" })


Note: I am trying to sort the list in alphabetical order

Answer
var cust = item.Profiles.Select(c => new
                {
                    id = c.CustId,
                    Name = c.Name

                }).OrderByDescending(c=>c.Name).ToList();

Or

 var cust = item.Profiles.OrderByDescending(a=>a.Name).Select(c => new
            {
                id = c.CustId,
                Name = c.Name

            }).ToList();
Comments