using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Telerik.Web.UI;
using System.Collections;
using Telerik.Web.UI.Skins;

public partial class Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }

    protected void RadButton1_Click(object sender, EventArgs e)
    {
        switch (RadComboBox1.Sort)
        {
            case RadComboBoxSort.None:
                RadComboBox1.Sort = RadComboBoxSort.Ascending;
                break;
            case RadComboBoxSort.Ascending:
                RadComboBox1.Sort = RadComboBoxSort.Descending;
                break;
            case RadComboBoxSort.Descending:
                RadComboBox1.Sort = RadComboBoxSort.Ascending;
                break;
        }

        //RadComboBox1.Sort = RadComboBoxSort.Descending;
        RadComboBox1.SortItems(new SortComboItemsByValue());
        RadComboBox1.OpenDropDownOnLoad = true;
    }

    protected void RadComboBox1_ItemDataBound(object sender, RadComboBoxItemEventArgs e)
    {
        DataRowView dataItem = (DataRowView)e.Item.DataItem;

        //add custom attributes to the items
        e.Item.Attributes["Freight"] = dataItem["Freight"].ToString();
        e.Item.Attributes["ShipName"] = dataItem["ShipName"].ToString();
        e.Item.Attributes["ShipCountry"] = dataItem["ShipCountry"].ToString();
    }
}


public class SortComboItemsByValue : IComparer
{
    public int Compare(object x, object y)
    {
        RadComboBoxItem p1 = new RadComboBoxItem();
        RadComboBoxItem p2 = new RadComboBoxItem();
        if (x is RadComboBoxItem)
            p1 = x as RadComboBoxItem;
        else
            throw new ArgumentException("Object is not of type RadComboBoxItem.");
        if (y is RadComboBoxItem)
            p2 = y as RadComboBoxItem;
        else
            throw new ArgumentException("Object is not of type RadComboBoxItem.");
        
        double cmp = 0;

        if (p1.ComboBoxParent.Sort == RadComboBoxSort.Ascending)
        {
            //here we compare the Values of the items
            //cmp = String.Compare(p1.Value, p2.Value, !p1.ComboBoxParent.SortCaseSensitive);

            //compare custom attribute
            cmp = Double.Parse(p1.Attributes["Freight"].ToString()) - Double.Parse(p2.Attributes["Freight"].ToString());
        }
        if (p1.ComboBoxParent.Sort == RadComboBoxSort.Descending)
        {
            //here we compare the Values of the items
            //cmp = String.Compare(p1.Value, p2.Value, !p1.ComboBoxParent.SortCaseSensitive) * -1;

            //compare custom attribute
            cmp = Double.Parse(p2.Attributes["Freight"].ToString()) - Double.Parse(p1.Attributes["Freight"].ToString());
        }
        return (int)cmp;
    }
}
