Sub topic 7
Ranking and sorting results
This chapter assumes basic familiarity with web or mobile interfaces, lists, forms, and TypeScript. You should also understand that a search request produces a set of matching items, while the interface decides how to order and display them.
Ranking and sorting solve different problems
Ranking orders results by how well they match the user's intent. A search for winter boots might place waterproof boots with strong text matches first, even if they are not the cheapest items.
Sorting orders the current result set by an explicit property, such as price, rating, date, or name. The user chooses the rule.
Use ranking as the default when users have a goal to satisfy. Offer sorting when users need to compare a known set of items.
| User need | Best default | Useful options |
|---|---|---|
| Find the most relevant product | Relevance ranking | Price, rating |
| Browse recently published articles | Date descending | Relevance, oldest |
| Compare products within a budget | Current filters plus a stable sort | Price low to high |
| Locate a known record | Exact or close match first | Name, date |
Do not treat “sort by relevance” as an ordinary sort control. Relevance depends on the query, available fields, business rules, and sometimes user context.
Build a predictable ranking model
A ranking model assigns each matching item a score. The score can combine several signals:
- How closely the title matches the query.
- Whether important fields, such as category or brand, match.
- Whether the item is available.
- Quality signals such as rating or popularity.
- Business rules, such as sponsored placement, clearly labeled.
Keep business rules bounded. A highly profitable item should not silently outrank an exact match if that prevents users from finding what they asked for.
Use deterministic tie-breakers. If two items have the same relevance score, order them by rating, then by item ID or another stable value. Stable ordering prevents items from jumping between pages after each request.
type Product = {
id: string;
name: string;
inStock: boolean;
rating: number;
popularity: number;
};
function rankProducts(products: Product[], query: string): Product[] {
const normalized = query.trim().toLowerCase();
return [...products]
.map((product) => {
const name = product.name.toLowerCase();
const exact = name === normalized ? 100 : 0;
const startsWith = name.startsWith(normalized) ? 40 : 0;
const contains = name.includes(normalized) ? 20 : 0;
const stock = product.inStock ? 10 : 0;
const score = exact + startsWith + contains + stock;
return { product, score };
})
.filter(({ score }) => score > 0)
.sort(
(a, b) =>
b.score - a.score ||
Number(b.product.inStock) - Number(a.product.inStock) ||
b.product.rating - a.product.rating ||
a.product.id.localeCompare(b.product.id),
)
.map(({ product }) => product);
}
const products: Product[] = [
{ id: "b2", name: "Winter Boots", inStock: true, rating: 4.4, popularity: 90 },
{ id: "a1", name: "Boot Care Kit", inStock: true, rating: 4.8, popularity: 70 },
];
console.log(rankProducts(products, "winter boots"));Make sorting clear and reversible
Show the active sort in the control itself. Labels such as “Price: low to high” are clearer than a generic “Price” label. Preserve the selected sort when users change a filter or move between result pages.
A sort should apply only to the current matching set. It should not remove the query or filters. If the chosen sort cannot produce a useful order, explain why instead of silently switching modes.
Exercise
A user searches for “red running shoes” and chooses “price: low to high.” Should the application discard relevance ranking?
Show answer
No. Filter or rank the matching shoes first, then apply the requested price order to that result set. Preserve the query and other active filters.