Click here to Skip to main content
15,893,668 members

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

Member 8090436 asked:

Open original thread
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)
        {
            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();
        }
    }
Tags: C#

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900