Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 430 Vote(s) - 3.44 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to Sort a List<T> by a property in the object

#1
I have a class called `Order` which has properties such as `OrderId`, `OrderDate`, `Quantity`, and `Total`. I have a list of this `Order` class:

```c#
List<Order> objListOrder = new List<Order>();
GetOrderList(objListOrder); // fill list of orders
```

I want to sort the list based on one property of the `Order` object; for example, either by `OrderDate` or `OrderID`.

How can I do this in C#?
Reply

#2
Using LINQ

objListOrder = GetOrderList()
.OrderBy(o => o.OrderDate)
.ToList();

objListOrder = GetOrderList()
.OrderBy(o => o.OrderId)
.ToList();
Reply

#3
Make use of LiNQ [`OrderBy`][1]

List<Order> objListOrder=new List<Order> ();
objListOrder=GetOrderList().OrderBy(o=>o.orderid).ToList();


[1]:

[To see links please register here]

Reply

#4
To do this without LINQ on .Net2.0:

List<Order> objListOrder = GetOrderList();
objListOrder.Sort(
delegate(Order p1, Order p2)
{
return p1.OrderDate.CompareTo(p2.OrderDate);
}
);

If you're on .Net3.0, then LukeH's [answer][1] is what you're after.

To sort on multiple properties, you can still do it within a delegate. For example:

orderList.Sort(
delegate(Order p1, Order p2)
{
int compareDate = p1.Date.CompareTo(p2.Date);
if (compareDate == 0)
{
return p2.OrderID.CompareTo(p1.OrderID);
}
return compareDate;
}
);

This would give you **ascending** dates with **descending** orderIds.

However, I wouldn't recommend sticking delegates as it will mean lots of places without code re-use. You should implement an `IComparer` and just pass that through to your `Sort` method. See [here][2].


public class MyOrderingClass : IComparer<Order>
{
public int Compare(Order x, Order y)
{
int compareDate = x.Date.CompareTo(y.Date);
if (compareDate == 0)
{
return x.OrderID.CompareTo(y.OrderID);
}
return compareDate;
}
}

And then to use this IComparer class, just instantiate it and pass it to your Sort method:

IComparer<Order> comparer = new MyOrderingClass();
orderList.Sort(comparer);


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#5
//Get data from database, then sort list by staff name:

List<StaffMember> staffList = staffHandler.GetStaffMembers();

var sortedList = from staffmember in staffList
orderby staffmember.Name ascending
select staffmember;
Reply

#6
// Totally generic sorting for use with a gridview

public List<T> Sort_List<T>(string sortDirection, string sortExpression, List<T> data)
{

List<T> data_sorted = new List<T>();

if (sortDirection == "Ascending")
{
data_sorted = (from n in data
orderby GetDynamicSortProperty(n, sortExpression) ascending
select n).ToList();
}
else if (sortDirection == "Descending")
{
data_sorted = (from n in data
orderby GetDynamicSortProperty(n, sortExpression) descending
select n).ToList();

}

return data_sorted;

}

public object GetDynamicSortProperty(object item, string propName)
{
//Use reflection to get order type
return item.GetType().GetProperty(propName).GetValue(item, null);
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through