Reflection get object property to sort the list

I want to sort a list in C # using the property of the objects stored in it. I have it:

if (sortColumn == "Login") { if (sortDir == "ASC") { filteredList.Sort((x, y) => string.Compare(x.Login, y.Login, true)); } else { filteredList.Sort((x, y) => string.Compare(y.Login, x.Login, true)); } } 

And it works great, but I want to make it more general so as not to know the sort field. I mean something like this:

 //With sortColumn = "Login"; if (sortDir == "ASC") { filteredList.Sort((x, y) => string.Compare(x.GetType().GetProperty(sortColumn), y.GetType().GetProperty(sortColumn), true)); } else { filteredList.Sort((x, y) => string.Compare(y.GetType().GetProperty(sortColumn), x.GetType().GetProperty(sortColumn), true)); } 

Obviously this does not work, but that is what I want. Is this possible in any way?

Thanks.

+7
source share
4 answers

reflection code is wrong look at this

 PropertyInfo pi1 = typeof(x).GetProperty(sortColumn); PropertyInfo pi2 = typeof(y).GetProperty(sortColumn); //With sortColumn = "Login"; if (sortDir == "ASC") { filteredList.Sort((x, y) => string.Compare(pi1.GetValue(x, null), pi2.GetValue(y, null), true)); } else { filteredList.Sort((x, y) => string.Compare(pi2.GetValue(y, null), pi1.GetValue(x, null), true)); } 

I think this will work for you.

+3
source

This is what I use for the same problem.

The usage looks like this: mySequence.OrderByPropertyName("Login", SortDirection.Descending) .

 public enum SortDirection { Ascending, Descending } public static IOrderedEnumerable<T> OrderByPropertyName<T> ( this IEnumerable<T> items, string propertyName, SortDirection sortDirection = SortDirection.Ascending ) { var propInfo = typeof(T).GetProperty(propertyName); return items.OrderByDirection(x => propInfo.GetValue(x, null), sortDirection); } public static IOrderedEnumerable<T> OrderByDirection<T, TKey> ( this IEnumerable<T> items, Func<T, TKey> keyExpression, SortDirection sortDirection = SortDirection.Ascending ) { switch (sortDirection) { case SortDirection.Ascending: return items.OrderBy(keyExpression); case SortDirection.Descending: return items.OrderByDescending(keyExpression); } throw new ArgumentException("Unknown SortDirection: " + sortDirection); } 
+1
source

I checked on dateTime and worked correctly.

  List<DateTime> list = new List<DateTime>(); list.Add(DateTime.Now); list.Add(DateTime.UtcNow.AddYears(2)); list.Sort((x, y) => (Convert.ToString(x.GetType().GetProperty("DayOfYear").GetValue(x)).CompareTo(Convert.ToString(y.GetType().GetProperty("DayOfYear").GetValue(y))))); 
0
source

Having deployed from the verdesmarald post, I split Ascending and Descending into separate methods and added ThenBy methods:

 using System.Collections.Generic; namespace System.Linq { public static class IEnumerableExtensions { enum SortDirection { Ascending, Descending } public static IOrderedEnumerable<T> OrderBy<T> (this IEnumerable<T> items, string propertyName) { var propInfo = typeof (T).GetProperty(propertyName); return items.OrderByDirection(x => propInfo.GetValue(x, null), SortDirection.Ascending); } public static IOrderedEnumerable<T> ThenBy<T> (this IOrderedEnumerable<T> items, string propertyName) { var propInfo = typeof(T).GetProperty(propertyName); return items.ThenByDirection(x => propInfo.GetValue(x, null), SortDirection.Ascending); } public static IOrderedEnumerable<T> OrderByDescending<T> (this IEnumerable<T> items, string propertyName) { var propInfo = typeof(T).GetProperty(propertyName); return items.OrderByDirection(x => propInfo.GetValue(x, null), SortDirection.Descending); } public static IOrderedEnumerable<T> ThenByDescending<T> (this IOrderedEnumerable<T> items, string propertyName) { var propInfo = typeof(T).GetProperty(propertyName); return items.ThenByDirection(x => propInfo.GetValue(x, null), SortDirection.Descending); } private static IOrderedEnumerable<T> OrderByDirection<T, TKey> (this IEnumerable<T> items, Func<T, TKey> keyExpression, SortDirection sortDirection) { switch (sortDirection) { case SortDirection.Ascending: return items.OrderBy(keyExpression); case SortDirection.Descending: return items.OrderByDescending(keyExpression); } throw new ArgumentException("Unknown SortDirection: " + sortDirection); } private static IOrderedEnumerable<T> ThenByDirection<T, TKey> (this IOrderedEnumerable<T> items, Func<T, TKey> keyExpression, SortDirection sortDirection) { switch (sortDirection) { case SortDirection.Ascending: return items.ThenBy(keyExpression); case SortDirection.Descending: return items.ThenByDescending(keyExpression); } throw new ArgumentException("Unknown SortDirection: " + sortDirection); } } } 
0
source

All Articles