HomeDev GuideRecipesAPI Reference
Dev GuideAPI ReferenceUser GuideGitHubNuGetDev CommunityOptimizely AcademySubmit a ticketLog In
Dev Guide

Order Optimizely Graph results using C# SDK

Order Optimizely Graph results in the C# SDK with .OrderBy() and .ThenBy().

Use .OrderBy() to sort results by any indexed field. Chain .ThenBy() for secondary sorts.

OrderDirection is an enum in the Optimizely.Graph.Cms.Query.Implementation namespace.

Sort ascending

Sort results in ascending order:

await client
    .QueryContent<BlogPostPage>()
    .OrderBy(x => x.StartPublish, OrderDirection.Ascending)
    .GetAsContentAsync();

Sort descending

Sort results in descending order:

await client
    .QueryContent<BlogPostPage>()
    .OrderBy(x => x.StartPublish, OrderDirection.Descending)
    .GetAsContentAsync();

Sort by multiple fields

Use .ThenBy() to apply a secondary sort when primary values are equal:

await client
    .QueryContent<BlogPostPage>()
    .OrderBy(x => x.BlogCategory, OrderDirection.Ascending)
    .ThenBy(x => x.StartPublish, OrderDirection.Descending)
    .GetAsContentAsync();

This sorts alphabetically by category, then by newest first within each category.

Sort by numeric fields

await client
    .QueryContent<BlogPostPage>()
    .OrderBy(x => x.Rating, OrderDirection.Descending)
    .GetAsContentAsync();

Combine sort with filters and pagination

.OrderBy() composes freely with .Where(), .Skip(), .Limit(), and other query methods:

await client
    .QueryContent<BlogPostPage>()
    .Where(x => x.BlogCategory == "Tutorials")
    .OrderBy(x => x.StartPublish, OrderDirection.Descending)
    .Limit(10)
    .GetAsContentAsync();