-1

How do I order my list with people alphabetical? I have the people randomly in a list but I want it to be alpabetical ordered. How do I do that?

Here is my code:

@if (ViewBag.Roles.CanModify)
{
<div id="AlleLedenLijst" class="AlleLedenLijst" style="height: 20px;">
    <input type="text" class="form-control" placeholder="Zoek een lid" name="search" id="FilterLeden"><br />
    <p>Sleep naar een groep om deze toe te voegen</p>
    <ul id="catalog" style="height: 200px;">
        @foreach (var LedenA in Model.Groepen.AllMembers)
        {
            <li class="list-group-item" id="@LedenA.UserID"><a href="#" onclick="showPopup(@LedenA.UserID, @Model.Groepen.OrgID)">@LedenA.TVGS @LedenA.Anaam @LedenA.Vname</a></li>
        }
    </ul>
</div>
}
<div id="UserDetailsDiv" class="modal-dialog"></div>
David Ferenczy Rogožan
  • 23,966
  • 9
  • 79
  • 68
lucafj2j282j
  • 879
  • 3
  • 13
  • 32

1 Answers1

5

You can use OrderBy for that:

@foreach (var LedenA in Model.Groepen.AllMembers.OrderBy(x => x.Anaam))
{ }

If you want to sort on the first name after that, use ThenBy:

@foreach (var LedenA in Model.Groepen.AllMembers.OrderBy(x => x.Anaam).ThenBy(x => x.Vnaam))
{ }
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • Am I not already putting lastname in front of the firstname by saying, LedenA.TVGS, LedenA.Anaam, LedenA.Vname – lucafj2j282j Apr 01 '16 at 11:05
  • You want to sort, right? If you have two members with the same last name, you then sort by the first name. – Patrick Hofman Apr 01 '16 at 11:05
  • @Lucafraser the order in which you output the fields does not influence the order in which the `foreach` loops though the list. By adding a `OrderBy` inside that foreach, you *do* change the sort order. – Hans Kesting Apr 01 '16 at 11:15