2012-08-31 8 views
7

リストをc#でソートするには、そのオブジェクトに格納されているオブジェクトのプロパティでソートします。リストをソートするリフレクションgetオブジェクトのプロパティ

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)); 
    } 
} 

そして、それが正常に動作しますが、私は、ソートするフィールドを知っている必要はないようにするためには、より一般的なやりたい:私はこれ持っています。私はこのようなことを考えている:

//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)); 
} 

明らかにこれは機能しませんが、これは私が望むものです。それはどんな方法でも可能ですか?

ありがとうございました。

+2

あなたは '.... GetPropertyの(sortColumn)以降.getvalue(...)を試してみました'? –

答えて

3

反射コードは、私はこれがあなたのために働くだろうと思い、この

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)); 
} 

を見て、正しくありません。

+1

ありがとうH-Bahrami !!これは私には少し修正が加えられましたが、戻り値を文字列にするために、 "pi1.GetValue(x、null)"と "pi2.GetValue(y、null)"に明示的なキャストを追加する必要がありました:string.Compare((string)pi1.GetValue(x、null)、(string)pi2.GetValue(y、null)、true)。 – christiansr85

1

これは私が同じ問題に使用しているものです。

使用状況は次のようになります。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

'OrderByDirection' ???? –

+0

@ L.Bハ、私はそれが拡張だったことを忘れてしまった、申し訳ありません。更新しました!また、direction引数はおそらく 'enum'でなければなりません。 – verdesmarald

+0

少しきれいにしました。 – verdesmarald

0

私はdateTimeに対して正常に動作していることを確認しました。 verdesmaraldポストのオフ拡大

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

私は昇順、別の方法に降順に分離し、ThenByメソッドを追加しました:

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); 
     } 
    } 
} 
関連する問題