Click here to Skip to main content
14,880,257 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have a list of my objects that have some properties. I want to sort my list by one of properties, just like ORDER BY in SQL. Please guide me..
Posted

You're looking for Linq to Objects[^].

Example:

userList = userList.OrderBy( user => user.LastName ).ToList();
   
v4
You can also have a look at the sort methods as documented here.
   
v2
try this code

C#
public class TMain
    {
        int iValue=0;
        string sValue = "";
        public int param1
        {
            get { return iValue; }
            set { iValue = value; }
        }
        public string param2
        {
            get { return sValue; }
            set { sValue = value; }
        }
    }


private void main()
{
TMain X = new TMain();
X.param1 = 10;
X.param2 = "param1";
TMain X1 = new TMain();
X1.param1 = 1;
X1.param2 = "param2";
List<TMain> Tlist = new List<TMain>();
Tlist.Add(X);
Tlist.Add(X1);
Tlist.Sort(TCompare);

}
private int TCompare(TMain x, TMain y)
{
if (x.param1 > y.param1)
return x.param1;
else
return y.param1;
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




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