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

Order Optimizely Graph results using C# SDK

Learn how to order results in the C# SDK using C# SDK

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

The OrderDirection Enum lives 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();

Multiple sort fields

Use .ThenBy() to apply secondary sorting 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 first, then by newest-first within each category.

Sorting by numeric fields

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

Combining 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();