#6 Basic sorting of lists
Simple Sorting of Persons
Initial Code
The following code runs at the beginning and creates some variables/services used in the following samples.
OrderBy(EntityId)
This example shows A-Z ordering by a property which exists on all entities: EntityId
Output
- Douglas Adams (#4001)
- Terry Pratchett (#4002)
- Neil Gaiman (#4003)
- George Akerlof (#4007)
- Raphael Müller (not an author) (#4011)
- Ed Hardy (#4012)
OrderBy(FirstName)
This example shows A-Z ordering by a property which exists only on Person-entities. This is simple with dynamic
objects
Output
- Douglas Adams
- Ed Hardy
- George Akerlof
- Neil Gaiman
- Raphael Müller (not an author)
- Terry Pratchett
OrderByDescending(Birthday)
This example shows Z-A ordering by a property.
Output
- Raphael Müller (not an author) (3/1/2000)
- Neil Gaiman (11/10/1960)
- Douglas Adams (3/11/1952)
- Terry Pratchett (4/28/1948)
- Ed Hardy (1/1/1945)
- George Akerlof (6/17/1940)
#6 Basic sorting of lists
Source Code of this file
Below you'll see the source code of the file. Note that we're just showing the main part, and hiding some parts of the file which are not relevant for understanding the essentials. Click to expand the code
@inherits Custom.Hybrid.Razor14 @using ToSic.Razor.Blade; @using System.Linq; <!-- unimportant stuff, hidden --> <div @Sys.PageParts.InfoWrapper()> @Html.Partial("../shared/DefaultInfoSection.cshtml") <div @Sys.PageParts.InfoIntro()> <h2>Simple Sorting of Persons</h2> </div> @{ var persons = AsList(App.Data["Persons"]); } </div> <h3>OrderBy(EntityId)</h3> <p>This example shows A-Z ordering by a property which exists on all entities: EntityId</p> <ol> @foreach (var person in persons.OrderBy(p => p.EntityId)) { <li>@person.FirstName @person.LastName (#@person.EntityId)</li> } </ol> <h3>OrderBy(FirstName)</h3> <p>This example shows A-Z ordering by a property which exists only on Person-entities. This is simple with <code>dynamic</code> objects</p> <ol> @foreach (var person in persons.OrderBy(p => p.FirstName)) { <li>@person.FirstName @person.LastName</li> } </ol> <h3>OrderByDescending(Birthday)</h3> <p>This example shows Z-A ordering by a property.</p> <ol> @foreach (var person in persons.OrderByDescending(p => p.Birthday)) { <li>@person.FirstName @person.LastName (@person.Birthday.ToString("d"))</li> } </ol> @* Footer *@ @Html.Partial("../Shared/Layout/FooterWithSource.cshtml", new { Sys = Sys })