liveBook · Manning

Sorting algorithm

As explained in Building Web APIs with ASP.NET Core

Sorting is the ordering of query results according to one or more specified fields or expressions.

Sorting orders query results by one or more fields or expressions. In an EF Core API, it makes a large board-game response easier for clients to consume by returning records in a predictable or requested order. section 5.4 section 5.4.2

EF Core supports two practical approaches:

  • Database sorting appends an ORDER BY operation to the query, allowing the database engine to use indexes and caching while avoiding retrieval of every record. section 5.4.2
  • Programmatic sorting retrieves unordered records and orders them in server or client memory, which becomes especially expensive when combined with paging. section 5.4.2
  • OrderBy() handles a fixed lambda expression such as b => b.Name, but dynamic client-selected columns require additional handling because the standard overload does not accept a string. section 5.4.2
  • System.Linq.Dynamic.Core enables string-based sorting, such as .OrderBy($"{sortColumn} {sortOrder}"), without writing a conditional branch for every sortable column. section 5.4.2

Board-game endpoint

A GET endpoint can accept sortColumn = "Name" and sortOrder = "ASC", apply the dynamic ordering before Skip and Take, and return a sorted page of board games. A default sort column of Name keeps requests predictable when the client supplies no value. section 5.4.2

Figure 5.5 A long list of board games
05-05

A long, unordered board-game response makes the practical need for configurable sorting, paging, and filtering immediately visible. section 5.4

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Explains database versus in-memory sorting, OrderBy limitations, dynamic strings, package installation, and endpoint integration.
Examples
Uses board-game JSON results, OrderBy(b => b.Name), dynamic sortColumn and sortOrder parameters, and paged controller queries.
What it assumes
Assumes basic database manipulation concepts while teaching the implementation through LINQ rather than requiring specific SQL knowledge.
What you can do after
Apply it via EF Core and System.Linq.Dynamic.Core to build database-backed, client-configurable sorting for API endpoints.
How it connects
Connects sorting with EF Core, LINQ, IQueryable, paging, filtering, database indexes, and caching.

Also explained in

Compare how 18 books explain sorting algorithm →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.