New to Telerik UI for ASP.NET AJAX? Start a free 30-day trial
Implement Custom Sorting
By default, RadListBox sorts items by their Text property. This article will show you how to implement custom sorting - the items will be sorted by their Value property.
For this purpose, we will use the IComparer interface which exposes a method that compares two objects.
Here are the two steps that you need to perform:
- Create a class that implements IComparer for your custom sorting:
C#
public class SortListBoxItemsByValue : IComparer
{
public int Compare(object x, object y)
{
RadListBoxItem p1 = new RadListBoxItem();
RadListBoxItem p2 = new RadListBoxItem();
if (x is RadListBoxItem)
p1 = x as RadListBoxItem;
else
throw new ArgumentException("Object is not of type RadListBoxItem.");
if (y is RadListBoxItem)
p2 = y as RadListBoxItem;
else
throw new ArgumentException("Object is not of type RadListBoxItem.");
int cmp = 0;
if (p1.ListBox.Sort == RadListBoxSort.Ascending)
{
//here we compare the Values of the items
cmp = String.Compare(p1.Value, p2.Value, !p1.ListBox.SortCaseSensitive);
}
if (p1.ListBox.Sort == RadListBoxSort.Descending)
{
//here we compare the Values of the items
cmp = String.Compare(p1.Value, p2.Value, !p1.ListBox.SortCaseSensitive) * -1;
}
return cmp;
}
}
- Call the overloaded Items.Sort methods passing your IComparer as a parameter:
C#
RadListBox1.Sort = RadListBoxSort.Ascending;
RadListBox1.Items.Sort(new SortListBoxItemsByValue());
That's it. Now the ListBox items will be sorted by their Value instead of Text property.