| 
 | 1 | +---  | 
 | 2 | +title: Custom Sorting Order for Groups in DropDownList  | 
 | 3 | +description: Learn how to apply a custom order to groups in the DropDownList for Blazor.  | 
 | 4 | +type: how-to  | 
 | 5 | +page_title: How to Set a Custom Grouping Order in Blazor DropDownList  | 
 | 6 | +slug: dropdownlist-custom-sorting-order-groups  | 
 | 7 | +tags: dropdownlist, blazor, telerik, grouping, custom order  | 
 | 8 | +res_type: kb  | 
 | 9 | +ticketid: 1666981, 1579415  | 
 | 10 | +---  | 
 | 11 | + | 
 | 12 | +## Environment  | 
 | 13 | + | 
 | 14 | +<table>  | 
 | 15 | +    <tbody>  | 
 | 16 | +        <tr>  | 
 | 17 | +            <td>Product</td>  | 
 | 18 | +            <td>DropDownList for Blazor, <br /> AutoComplete for Blazor, <br /> MultiSelect for Blazor, <br /> ComboBox for Blazor, <br /> MultiColumnComboBox for Blazor</td>  | 
 | 19 | +        </tr>  | 
 | 20 | +    </tbody>  | 
 | 21 | +</table>  | 
 | 22 | + | 
 | 23 | +## Description  | 
 | 24 | + | 
 | 25 | +I am using the [grouping feature of the DropDownList]({%slug components/dropdownlist/grouping%}). My DropDownList model has a nested model as property. The DropDownList `GroupField` parameter is bound to a property of the nested model. I want the groups to appear in a non-alphabetical, custom order. I can see that there is a feature request to add a <a href="https://feedback.telerik.com/blazor/1565506-sort-direction-parameter-for-grouping" target="_blank">sort direction option when grouping</a>. But in the meantime how can I customize the grouping order in the DropDownList?  | 
 | 26 | + | 
 | 27 | +## Solution  | 
 | 28 | + | 
 | 29 | +To sort the groups in a DropDownList by a custom order, perform a manual sorting operation in the [`OnRead` event]({%slug components/dropdownlist/events%}#onread) handler. Follow these steps:  | 
 | 30 | + | 
 | 31 | +1. Create a list of strings that represents the values of the group headers. This list will determine the preferred sorting order.  | 
 | 32 | +2. Cast the [`DataSourceResult`]({%slug common-features-data-binding-onread%}#event-argument) to [`AggregateFunctionsGroup`](/blazor-ui/api/Telerik.DataSource.AggregateFunctionsGroup).  | 
 | 33 | +3. Sort the casted data using the <a href="https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.sort?view=net-8.0" target="_blank">`Sort` method</a> with a custom comparison function.  | 
 | 34 | +4. Pass the sorted data as [`args.Data`]({%slug common-features-data-binding-onread%}#todatasourceresult-method).  | 
 | 35 | + | 
 | 36 | +>caption Apply a custom grouping order in the DropDownList  | 
 | 37 | +````CSHTML  | 
 | 38 | +@using Telerik.DataSource.Extensions  | 
 | 39 | +@using Telerik.DataSource  | 
 | 40 | +
  | 
 | 41 | +<TelerikDropDownList TItem="@Product"  | 
 | 42 | +                     TValue="@int"  | 
 | 43 | +                     OnRead="@ReadItems"  | 
 | 44 | +                     @bind-Value="@SelectedValue"  | 
 | 45 | +                     GroupField="ProductCategory.CategoryName"  | 
 | 46 | +                     TextField="@nameof(Product.Description)"  | 
 | 47 | +                     ValueField="@nameof(Product.ProductId)"  | 
 | 48 | +                     DefaultText="Select a Product">  | 
 | 49 | +</TelerikDropDownList>  | 
 | 50 | +
  | 
 | 51 | +@code {  | 
 | 52 | +    private int SelectedValue { get; set; }  | 
 | 53 | +    private List<Product> Products = new List<Product>  | 
 | 54 | +        {  | 
 | 55 | +            new Product { ProductId = 10, Description = "Juliet Jewelery Product", ProductCategory = new ProductCategory { CategoryId = 10, CategoryName = "Jewelery" } },  | 
 | 56 | +            new Product { ProductId = 18, Description = "Medical Care Product", ProductCategory = new ProductCategory { CategoryId = 8, CategoryName = "Healthcare" } },  | 
 | 57 | +            new Product { ProductId = 19, Description = "QQ Tech Product", ProductCategory = new ProductCategory { CategoryId = 9, CategoryName = "Information Technology" } },  | 
 | 58 | +            new Product { ProductId = 10, Description = "Z Jewelery Product", ProductCategory = new ProductCategory { CategoryId = 10, CategoryName = "Jewelery" } },  | 
 | 59 | +            new Product { ProductId = 4, Description = "Delta Bond Product", ProductCategory = new ProductCategory { CategoryId = 4, CategoryName = "Defense" } },  | 
 | 60 | +            new Product { ProductId = 8, Description = "Health Care Product", ProductCategory = new ProductCategory { CategoryId = 8, CategoryName = "Healthcare" } },  | 
 | 61 | +            new Product { ProductId = 9, Description = "Tech Product", ProductCategory = new ProductCategory { CategoryId = 9, CategoryName = "Information Technology" } }  | 
 | 62 | +        };  | 
 | 63 | +
  | 
 | 64 | +    protected async Task ReadItems(DropDownListReadEventArgs args)  | 
 | 65 | +    {  | 
 | 66 | +        await Task.Delay(200);  | 
 | 67 | +
  | 
 | 68 | +        var preferredOrder = new List<string> { "Healthcare", "Information Technology", "Defense", "Jewelery" };  | 
 | 69 | +
  | 
 | 70 | +        var datasourceResult = Products.ToDataSourceResult(args.Request);  | 
 | 71 | +
  | 
 | 72 | +        var sortedData = datasourceResult.Data.Cast<AggregateFunctionsGroup>().ToList();  | 
 | 73 | +        sortedData.Sort((a, b) =>  | 
 | 74 | +        {  | 
 | 75 | +            int indexA = preferredOrder.IndexOf(a.Key.ToString());  | 
 | 76 | +            int indexB = preferredOrder.IndexOf(b.Key.ToString());  | 
 | 77 | +
  | 
 | 78 | +            if (indexA >= 0 && indexB >= 0)  | 
 | 79 | +            {  | 
 | 80 | +                return indexA.CompareTo(indexB);  | 
 | 81 | +            }  | 
 | 82 | +
  | 
 | 83 | +            if (indexA >= 0) return -1;  | 
 | 84 | +            if (indexB >= 0) return 1;  | 
 | 85 | +            return a.Key.ToString().CompareTo(b.Key.ToString());  | 
 | 86 | +        });  | 
 | 87 | +
  | 
 | 88 | +        args.Data = sortedData;  | 
 | 89 | +    }  | 
 | 90 | +
  | 
 | 91 | +    public class Product  | 
 | 92 | +    {  | 
 | 93 | +        public int ProductId { get; set; }  | 
 | 94 | +        public string Description { get; set; }  | 
 | 95 | +        public ProductCategory ProductCategory { get; set; }  | 
 | 96 | +    }  | 
 | 97 | +
  | 
 | 98 | +    public class ProductCategory  | 
 | 99 | +    {  | 
 | 100 | +        public int CategoryId { get; set; }  | 
 | 101 | +        public string CategoryName { get; set; }  | 
 | 102 | +    }  | 
 | 103 | +}  | 
 | 104 | +```  | 
 | 105 | +
  | 
 | 106 | +
  | 
 | 107 | +## See Also  | 
 | 108 | +
  | 
 | 109 | +- [Telerik DropDownList for Blazor - Overview]({%slug components/dropdownlist/overview%})  | 
 | 110 | +- [OnRead Event in Telerik DropDownList]({%slug components/dropdownlist/events%}#onread)  | 
 | 111 | +- [Grouping in Telerik DropDownList]({%slug components/dropdownlist/grouping%})  | 
 | 112 | +- [Custom Grouping Order in Grid]({%slug grid-custom-grouping-order%})  | 
0 commit comments