Click here to Skip to main content
15,889,761 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all,
i have a list that I extract from a database that looks like this:
id name   firstname adress   age classby order
1  Roger  Doe        Montana  45  name   ASC
2  Rambo  john       Miami    50  name   ASC
3  Doe    john       Montana  45  name   ASC

What I want from my program is that it looks in the "classby" field and orders it based on the value in the "order" field. In this example, I want to sort by name ascending.
id name firstname adress age classby order
3  Doe    john    Montana 45  name    ASC
2  Rambo  john    Miami   50  name    ASC
1  Roger  Doe     Montana 45  name    ASC
If i do something like this it will work? :
C#
foreach (List l in biglist)
{
  if (l.classby =="value") && (l.order == "ASC") //value gets name in this exemple
  {
    biglist.OrderBy(list => l.name);
  }
  else if ((l.classby =="value") && (l.order == "DESC") 
  {
    biglist.OrderByDescending(list => l.name);
  }
}
Posted
Updated 9-Sep-13 4:58am
v5
Comments
Harshil_Raval 9-Sep-13 7:45am    
Not clear. please describe your question. what exactly you want to do?
redfox06 9-Sep-13 8:48am    
Sry didnt realise my question wasn't clear. i updated it
Mike Meinz 9-Sep-13 8:21am    
What does "class my list" and "value inside class" mean?
Ron Beyer 9-Sep-13 10:52am    
Why not select it from the database in the order you want it to appear?
Mike Meinz 9-Sep-13 11:07am    
All you have to do is add the "ORDER BY" clause to your SQL SELECT statement and the items will be returned from the database to your application in the correct order.

You could - but there are two other ways which might be better. The first is to read the data back in teh order you want - that is a simple matter of adding an ORDER BY clause to your select statement:
SQL
SELECT * FROM myTable WHERE ... ORDER BY name ASC
That way the data is ordered the way you want it before you even see it.

The other alternative is to apply a sort to the display control you are using. For example, for a DataGridView:
C#
myDataGridView.Sort(myDataGridView.Columns["Name"],ListSortDirection.Ascending);


Or you could do it your way, except that neither of the OrderBy methods you are using modify the original input data - they both return a sorted IEnumerable instead:
C#
var sorted = biglist.OrderBy(item => item.name);
 
Share this answer
 
There are a couple of ways that you can achieve what you're after. First of all, you must realise that you have a fair bit of redundancy in there - you're repeating the ClassBy and Order fields on each item, even though you only need them once. After all, the behaviour would be wildly unpredictable if you were to try to change the sort order/column, throughout the list. Now, supposing that you flatten the data out into a class structure, you can use either Dynamic Linq or reflection to manage the sort. With DLINQ, your query might look something like this:
C#
MyDetailsList list = new MyDetailsList();
list.Populate();
var firstItem = list.First();
var outputList = null;
if (string.Compare(firstItem.OrderBy, "asc", StringComparison.InvariantCultureIgnoreCase) == 0)
{
  outputList = list.OrderBy(firstItem.ClassBy);
}
else
{
  outputList = list.OrderByDescending(firstItem.ClassBy);
}
Now, if you don't want to use DLINQ, you'd use something like this instead:
C#
MyDetailsList list = new MyDetailsList();
list.Populate();
var firstItem = list.First();
var outputList = null;
var propInfo = list.GetType().GetProperty(firstItem.ClassBy);
if (string.Compare(firstItem.OrderBy, "asc", StringComparison.InvariantCultureIgnoreCase) == 0)
{
  outputList = list.OrderBy(x => propInfo.GetValue(x, null));
}
else
{
  outputList = list.OrderByDescending(x => propInfo.GetValue(x, null));
}
 
Share this answer
 

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