How to Sort a List of Objects by a Specific Field in C#

How to Sort a list by field

You can use List.Sort

l.Sort((p, q) => p.Category.CompareTo(q.Category));

The advantage over the LINQ OrderBy is that you'll order the list in-place instead of generating an IOrderedEnumerable<T> that then you have to re-transform in a List<T>.

How to sort a list of objects by a specific field in C#?

Using LINQ:

var sortedList = _allStatInfo.OrderBy(si => si.date).ToList();

Sorting the original list:

_allStatInfo.Sort(new Comparison<StatInfo>((x, y) => DateTime.Compare(x.date, y.date)));

How to Sort a ListT by a property in the object

The easiest way I can think of is to use Linq:

List<Order> SortedList = objListOrder.OrderBy(o=>o.OrderDate).ToList();

How to sort (order by) list of objects according to specific column

Assuming your list is a list of a custom type and you want to sort by one or multiple properties:

var trans = transactions.OrderBy(t => t.PropertyName)
.ThenBy(t => t.DifferentPropertyName)

If you want to order descending instead:

var trans = transactions.OrderByDescending(t => t.PropertyName)
.ThenByDescending(t => t.DifferentPropertyName)

Enumerable.OrderBy Method

Sort List of C# object by STRING parameter

Add reference to nuget package:

https://www.nuget.org/packages/System.Linq.Dynamic/

  1. Add using System.Linq.Dynamic; at the top.
  2. Use var usersSorted = users.AsQueryable().OrderBy("firstname ASC").ToList();

Sort list by field (C#)

list.Sort((x,y) =>
x.m_valRating.CompareTo(y.m_valRating));

rearrange a list of objects by type field in C#

Solution #1 Order values array

I would just define the order of those ids in some kind of collection, can be an array:

var orderArray = new int[]
{
8106, // Confirmed Out of Busine
8105, // Bankruptcy
8111, // Lack of Licensing
8109, // Investigations
8103, // Government Actions
8104, // Pattern of Complaints
8112, // Customer Reviews
8110, // Accreditation
8101, // Misuse of BBB Name
8107, // Advisory
8102, // Advertising Review
};

Then iterate through array while incrementing order value. While looping check if order array contains actual type id which order value I'm trying to evaluate:

for (int orderValue = 0; orderValue < orderArray.Length; orderValue++)
{
if (alertTypeId == orderArray[orderValue])
{
return orderValue;
}
}

If not found in the array, return highest value possible:

return int.MaxValue

Whole method would look like this and it would evaluate the order for alert type id:

public int GetAlertTypeIdOrder(short alertTypeId)
{
var orderArray = new int[]
{
8106, // Confirmed Out of Busine
8105, // Bankruptcy
8111, // Lack of Licensing
8109, // Investigations
8103, // Government Actions
8104, // Pattern of Complaints
8112, // Customer Reviews
8110, // Accreditation
8101, // Misuse of BBB Name
8107, // Advisory
8102, // Advertising Review
};

for (int orderValue = 0; orderValue < orderArray.Length; orderValue++)
{
if (alertTypeId == orderArray[orderValue])
{
return orderValue;
}
}

return int.MaxValue;
}

Usage:

var sortedAlerts = alerts
.AllAlerts
.OrderByDescending(a => GetAlertTypeIdOrder(a.AlertTypeId))
.ToList();

It also works in a descending way.

Solution #2 Order values dictionary

You could achieve better performance by reducing the redundancy - repeated creation of array storing order values. Better idea would be to store the order rules in a dictionary. I know that code below creates an array too, but the concept is that it would be called once to get the dictionary which would be then passed over.

public Dictionary<int, int> GetOrderRules()
{
var alertTypeIds = new int[]
{
8106, // Confirmed Out of Busine
8105, // Bankruptcy
8111, // Lack of Licensing
8109, // Investigations
8103, // Government Actions
8104, // Pattern of Complaints
8112, // Customer Reviews
8110, // Accreditation
8101, // Misuse of BBB Name
8107, // Advisory
8102, // Advertising Review
};

var orderRules = new Dictionary<int, int>();

for (int orderValue = 0; orderValue < alertTypeIds.Length; orderValue++)
{
orderRules.Add(alertTypeIds[orderValue], orderValue);
}

return orderRules;
}

So the GetAlertIdOrder() method would look different, but still keeping the idea from previous solution:

public int GetAlertIdOrder(short alertTypeId, IDictionary<int, int> orderRules)
{
if (orderRules.TryGetValue(alertTypeId, out int orderValue))
{
return orderValue;
}
else
{
return int.MaxValue;
}
}

Usage:

var orderRules = GetOrderRules();
var sortedAlerts = alerts
.AllAlerts
.OrderBy(a => GetAlertIdOrder(a.AlertTypeId, orderRules))
.ToList();

Sort a list of objects by the value of a property

Use OrderBy of Linq function. See http://msdn.microsoft.com/en-us/library/bb534966.aspx

cities.OrderBy(x => x.population);


Related Topics



Leave a reply



Submit