Sortable Property Values
Learn how to enable sorting for custom property editors that store complex values like JSON in Umbraco collection views.
Last updated
Was this helpful?
Was this helpful?
public interface IDataValueSortable
{
string? GetSortableValue(object? value, object? dataTypeConfiguration);
}{"name":"Awesome Widget","setting":"advanced"}using Umbraco.Cms.Core.IO;
using Umbraco.Cms.Core.PropertyEditors;
using Umbraco.Cms.Core.Serialization;
using Umbraco.Cms.Core.Strings;
public class MyWidgetDataValueEditor : DataValueEditor, IDataValueSortable
{
private readonly IJsonSerializer _jsonSerializer;
public MyWidgetDataValueEditor(
IShortStringHelper shortStringHelper,
IJsonSerializer jsonSerializer,
IIOHelper ioHelper,
DataEditorAttribute attribute)
: base(shortStringHelper, jsonSerializer, ioHelper, attribute)
{
_jsonSerializer = jsonSerializer;
}
public string? GetSortableValue(object? value, object? dataTypeConfiguration)
{
if (value is not string stringValue || string.IsNullOrWhiteSpace(stringValue))
{
return null;
}
WidgetValue? widget = _jsonSerializer.Deserialize<WidgetValue>(stringValue);
// Extract the name and normalize to lowercase for case-insensitive sorting.
return widget?.Name?.ToLowerInvariant();
}
private class WidgetValue
{
public string? Name { get; set; }
public string? Setting { get; set; }
}
}