开发者

How to sort a user Created List<UserClass> Collection in C#

I wish to sort the collection List<UserClass>

on the basis of one of the property among various of that UserClass which is User Defined.

Is it valid

List&开发者_开发问答lt;Function> objFunctionsList = new List<Function>(); 

// populating data in objFunctionsList 

objFunctionsList = objFunctionsList.OrderBy(x => x.Name).ToList();


Just use the linq extension method orderby

var sorted=list.OrderBy(x=>x.MyProperty);

If you need a list as a result then add ToList() eg

var sortedList=list.OrderBy(x=>x.MyProperty).ToList();

Alternatively you could use this Extension class

public static class SortExtension {

  public static Comparison<T> GetComparer<T, TP>(Expression<Func<T, IComparable<TP>>> propertyExpression) {

    if (propertyExpression == null) throw new ArgumentNullException();
    var member = ((propertyExpression.Body is UnaryExpression) ? ((UnaryExpression)propertyExpression.Body).Operand : propertyExpression.Body) as MemberExpression;
    if (member == null) throw new ArgumentException();
    var parameterA = Expression.Parameter(typeof(T), "a");
    var parameterB = Expression.Parameter(typeof(T), "b");
    var nullExpr = Expression.Constant(null);
    var valueA = Expression.Property(parameterA, member.Member.Name);
    var valueB = Expression.Property(parameterB, member.Member.Name);
    var compare = Expression.Call(valueA, typeof(TP).GetMethod("CompareTo", new[] { typeof(TP) }), valueB);
    var checkBPropNull = Expression.Condition(Expression.Equal(valueB, nullExpr), Expression.Constant(0), Expression.Constant(-1));
    var checkAPropertyNull = Expression.Condition(Expression.Equal(valueA, nullExpr), checkBPropNull, compare);
    var checkBNullANot = Expression.Condition(Expression.Equal(parameterB, nullExpr), Expression.Constant(1), checkAPropertyNull);
    var checkBNullANull = Expression.Condition(Expression.Equal(parameterB, nullExpr), Expression.Constant(0), Expression.Constant(-1));
    var checkANull = Expression.Condition(Expression.Equal(parameterA, nullExpr), checkBNullANull, checkBNullANot);
    return (a, b) => Expression.Lambda<Func<T, T, int>>(checkANull, parameterA, parameterB).Compile()(a, b);
  }

  public static void SortBy<T, TP>(this List<T> source, Expression<Func<T, IComparable<TP>>> propertyExpression) {
    if (source == null) throw new ArgumentNullException();
    source.Sort(GetComparer(propertyExpression));
  }
}

Then you can just do

list.SortBy(x=>x.MyProperty);

The Expression building produces a comparitor that is functionally equivalent to

list.Sort((a,b) => {
  if (a == null) return (b==null) ? 0 :-1;
  if (b==null) return 1;
  if (a.MyProperty == null) return (b.MyProperty==null) ? 0 : -1;
  return a.T1.CompareTo(b.T1);
});


users.Sort((u1, u2) => {
    return u1.Age.CompareTo(u2.Age);
});

This will sort by Age for example.


If your list is list, then:

list.Sort((a, b) => {
                       if (a==null) return (b==null) ? 0 : -1;
                       if (b==null) return 1;
                       if (a.Property==null) return (b.Property==null) ? 0 : -1;
                       return a.Property.CompareTo(b.Property);
                     });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜