Custom `ISortHandler` for use with the Delivery AP...
# help-with-umbraco
p
I need to implement a custom sort for Delivery API content, I've implemented it as follows:
Copy code
csharp
public class ReleaseDateSort : ISortHandler
{
    private const string SortOptionSpecifier = "releaseDate:";
    
    /// <inheritdoc />
    public bool CanHandle(string query)
        => query.StartsWith(SortOptionSpecifier, StringComparison.OrdinalIgnoreCase);

    public SortOption BuildSortOption(string sort)
    {
        var sortDirection = sort.Substring(SortOptionSpecifier.Length);

        return new SortOption
        {
            FieldName = ReleaseDateSortIndexer.FieldName,
            Direction = sortDirection.StartsWith("asc", StringComparison.OrdinalIgnoreCase)
                ? Direction.Ascending
                : Direction.Descending
        };
    }
}
However, my sort does not appear to be applied. I am passing it as
releaseDate:asc
. My breakpoint is hit in the
BuildSortOption
method of my
ISortHandler
implementation. Am I missing something?
My indexer looks like this:
Copy code
csharp
public sealed class ReleaseDateSortIndexer : IContentIndexHandler
{
    internal const string FieldName = "releaseDate";
    
    /// <inheritdoc />
    public IEnumerable<IndexFieldValue> GetFieldValues(IContent content, string? culture)
    {
        var releaseDate = content.GetValue<DateTime?>(FieldName);
        
        if (releaseDate.HasValue)
        {
            return [new IndexFieldValue
                {
                    FieldName = FieldName,
                    Values = [releaseDate]
                }
            ];
        }

        if (content.PublishDate.HasValue)
        {
            return
            [
                new IndexFieldValue
                {
                    FieldName = FieldName,
                    Values = [content.PublishDate]
                }
            ];
        }

        return [];
    }

    /// <inheritdoc />
    public IEnumerable<IndexField> GetFields() =>
    [
        new()
        {
            FieldName = FieldName, FieldType = FieldType.Date, VariesByCulture = false
        }
    ];
}
As far as I can tell this is all as per the docs and the Core sorting logic.
18 Views