Mangesh barmate

Mangesh barmate

  • NA
  • 118
  • 132.6k

How to apply SORTING to LIST COLLECTION in C#.Net?

Jan 31 2013 9:39 AM
Below are my Highlighted code there i want to apply sorting on any column-->
{
    public class SalesPlanProperty
    {
        private int _ID;
        private string _fieldName;
        private string _valueType;
        private DateTime _JoiningDate;
        public int ID
        {
            get { return _ID; }
            set{_ID=value;}
        }
        public string fieldName
        {
            get{ return _fieldName;}
            set{_fieldName = value;}
        }
        public string ValueType
        {
            get { return _valueType; }
            set { _valueType = value; }
        }
        public DateTime JoiningDate
        {
            get { return _JoiningDate; }
            set { _JoiningDate = value; }
        }
    }
}
List<SalesPlanProperty> propertyList = new List<SalesPlanProperty>();
        protected void Page_Load(object sender, EventArgs e)
        {  }
        public void SortdListByParam(object strFiled)
        {
< big>            propertyList.OrderBy(x => strFiled).ToList(); // HERE I WANT TO SORT MY COLLECTION ON ANY PROPERTY VAILABLE IN MY CLASS.</big>
        }

        public List<SalesPlanProperty> CreateCollection()
        {
            for (int i = 1; i <= 500; i++)
            {
                var data = new SalesPlanProperty { ID = i, fieldName = "Field" + i.ToString(), ValueType = "Value" + i.ToString(), JoiningDate = DateTime.Now.AddSeconds(i)  };
                propertyList.Add(data);
            }
            return propertyList;
        }

        public void getdata(int pageNo, object myDictionary,object strSortFieldName)
        {
            List<SalesPlanProperty> KeyValuePair=((List<SalesPlanProperty>) myDictionary);
            GridView1.DataSource = KeyValuePair;
            GridView1.DataBind();
            int low = (pageNo - 1) * 100;
            int high = low + 100;
            this.SortdListByParam(strSortFieldName);
            foreach(SalesPlanProperty spp in KeyValuePair)
            {
                int Idvalue = spp.ID;
                if (Idvalue > low && Idvalue <= high)
                {
                    Response.Write("ID " + Idvalue + "          ");
                    Response.Write("FieldName  " + spp.fieldName + "     ");
                    Response.Write("Value   " +spp.ValueType + "       ");
                }
            }
        }

        protected void btnCollection_Click(object sender, EventArgs e)
        {
            object strSortFieldNameValue = "JoiningDate";//Pass column name on which it is sort.
            this.getdata(2, this.CreateCollection(), strSortFieldNameValue);//Pass Page number index,Create collection and column name on which it is sort.
        }

       protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            List<SalesPlanProperty> KeyValuePair = this.CreateCollection();
            GridView1.DataSource = KeyValuePair;
            GridView1.DataBind();
        }
    }

Answers (4)