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 a ListT by a property in the object, according to a comma separated string of the property
I couldn't find a solution with OrderBy
. But here what I have come up with.
First, scan sortOrderList
and add the products that match the condition in the sortedProducts
list(You can write re-write this part with LINQ if you like). Then, take all the products from the original list that don't exist in the new one, and add them as well.
IList<int> sortOrderList = sortOrder.Split(",").Select(Int32.Parse).ToList();
var sortedProducts = new List<Product>();
foreach (var id in sortOrderList)
{
sortedProducts.Add(products.First(p => p.Id == id));
}
sortedProducts.AddRange(products.Where(p => sortedProducts.Exists(sp => sp.Id == p.Id) == false));
Don't forget to add support for sort order IDs that don't exist in the original list. IE you should skip those.
How to Sort a List by a property in the object
You can use LINQ to get new list with items sorted:
var sortedFactors = factors.OrderBy(x => x.explain).ToList();
or you can sort it in-place using List<T>.Sort
method:
factors.Sort((e1, e2) => e1.explain.CompareTo(e2.explain));
Update
Using List<T>.Sort
with date
property is a little bit more tricky, because its Nullable<DateTime>
, and it's not really obvious how to order null
and non-null
values.
factors.Sort((e1, e2) => e1.date.HasValue ? (e2.date.HasValue ? e1.date.Value.CompareTo(e2.date.Value) : -1) : ((e2.date.HasValue ? 1 : 0)));
C# Sort ListT by its property which is string of datetime
Based on your comments on the answers you are looking for a reflection based solution.
The reflection part, to get the value of a property based on the name:
x.GetType().GetProperty(propertyName).GetValue(x, null)
Try this:
var propetyName = "StringAsDate";
var listSorted = list.OrderByDescending(x => DateTime.Parse(Convert.ToString(x.GetType().GetProperty(propertyName).GetValue(x, null))));
If the Property
is already of type DateTime
a cast would be sufficient
var propetyName = "StringAsDate";
var listSorted = list.OrderByDescending(x => (DateTime)x.GetType().GetProperty(propertyName).GetValue(x, null));
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);
Sort a ListT by an unknown property of an object
Try to use nuget package System.Linq.Dynamic.Core (https://dynamic-linq.net/overview) and you can pass just string to orderBy method.
for example that how it looks in our generic method.
query.OrderBy(input.Sorting ?? "id asc")
it accepts a string from generic input or if it null we order with Id column by default.
Sort ListT using the property of its property
Make some changes to Person class:
public class Person : IComparable {
public string Name {get;set;}
public Guid PersonID {get;set;}
// implement the methods for IComparable
}
you can find a good sample in this link.
https://msdn.microsoft.com/en-us/library/system.icomparable(v=vs.110).aspx
Sort ListT by count of property of inner list
I think you need the following- (if not then let me know)
List<ComputerCard> SortedCards = cards.OrderBy(
x => x.CardRoles.Sum(y => y.DependentRoles.Count))
.ToList();
foreach (var item in SortedCards)
{
item.CardRoles = item.CardRoles
.OrderByDescending(x => x.DependentRoles.Count).ToList();
}
How to sort a list of objects by a property of a nested list with Linq?
You can use OrderBy
method with Min
age of Members
list of each group
var groups = new List<Group>
{
new Group {Name = "group1", Members = new List<Person> {new Person {Age = 5}, new Person {Age = 2}}},
new Group {Name = "group2", Members = new List<Person> {new Person {Age = 3}, new Person {Age = 4}}},
new Group {Name = "group3", Members = new List<Person> {new Person {Age = 1}}}
};
var result = groups.OrderBy(g => g.Members.Min(m => m.Age));
It gives you the group3 (minimal Age
is 1), group1 (minimal Age
is 2), group2 (minimal Age
is 3) order in example above
Related Topics
How to Simulate Mouse Click in C#
How to Convert Json to Xml or Xml to Json
Download File of Any Type in ASP.NET MVC Using Fileresult
Filesystemwatcher Changed Event Is Raised Twice
How to Dynamically Generate HTML Code Using .Net'S Webbrowser or Mshtml.Htmldocument
How to Strip HTML Tags from a String in Asp.Net
How to Remove All HTML Tags from a String Without Knowing Which Tags Are in It
Convert Webpage to Image from Asp.Net
Html Agility Pack - Parsing Tables
How to Find the Text Within a Div in the Source of a Web Page Using C#
How to Convert HTML to Text in C#
Generating HTML Email Body in C#
An Existing Connection Was Forcibly Closed by the Remote Host