#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.
@{
var persons = AsList(App.Data["Persons"]);
}
OrderBy(EntityId)
This example shows A-Z ordering by a property which exists on all entities: EntityId
Output
- Douglas Adams (#33125)
- Terry Pratchett (#33126)
- Neil Gaiman (#33127)
- George Akerlof (#33131)
- Raphael Müller (not an author) (#33135)
- Ed Hardy (#33136)
<ol>
@foreach (var person in persons.OrderBy(p => p.EntityId)) {
<li>@person.FirstName @person.LastName (#@person.EntityId)</li>
}
</ol>
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
<ol>
@foreach (var person in persons.OrderBy(p => p.FirstName)) {
<li>@person.FirstName @person.LastName</li>
}
</ol>
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)
<ol>
@foreach (var person in persons.OrderBy(p => p.FirstName)) {
<li>@person.FirstName @person.LastName</li>
}
</ol>
#6 Basic sorting of lists
@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 })