Hi, is it possible to sort items in the RadListControl by item value instead of item text?
Thanks,
-Lou
2 Answers, 1 is accepted
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 23 Mar 2015, 10:52 AM
Hello Lou,
Thank you for writing.
Sorting in RadListControl is controlled only via the SortStyle property. It supports ascending sort style, descending sort style and no sort style. Setting sorting through code looks like this:
So, if you want to use custom comparer method for sorting operation you should use this approach:
public Form1()
{
InitializeComponent();
List<Item> items = new List<Item>();
for (int i = 0; i < 20; i++)
{
items.Add(new Item(i, i + ".Item"));
}
this.radListControl1.DataSource = items;
this.radListControl1.DisplayMember = "Name";
this.radListControl1.ValueMember = "Id";
List<RadListDataItem> sortList = new List<RadListDataItem>(radListControl1.Items);
sortList.Sort(new MyComparer());
radListControl1.Items.Clear();
radListControl1.Items.AddRange(sortList);
}
public class MyComparer : IComparer <RadListDataItem>
{
public int Compare(RadListDataItem x, RadListDataItem y)
{
Item xItem = x.DataBoundItem as Item;
Item yItem = y.DataBoundItem as Item;
return xItem.Id.CompareTo(yItem.Id);
}
}
public class Item
{
public int Id { get; set; }
public string Name { get; set; }
public Item()
{
}
public Item(int id, string name)
{
this.Id = id;
this.Name = name;
}
}
I hope this information helps. Should you have further questions, I would be glad to help.
Regards,
Dess
Telerik
Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.