/// <summary> /// Sorts the source collection using custom sort criteria. /// </summary> /// <remarks> /// Please note that your compare function needs to take care about /// proper conversion of types to be comparable! /// </remarks> /// <param name="source"> /// The source collection to sort. /// </param> /// <param name="args"> /// Sort criteria to use. /// </param> /// <returns> /// A sorted array containing collection elements. /// </returns> public object Execute(ICollection source, object[] args) { if (source == null || source.Count == 0) { return(source); } if (args == null || args.Length != 1) { throw new ArgumentException("compare expression is a required argument for orderBy"); } var arg = args[0]; IComparer comparer = null; if (arg is string) { var expCompare = Expression.Parse((string)arg); comparer = new SimpleExpressionComparer(expCompare); } else if (arg is IComparer) { comparer = (IComparer)arg; } else if (arg is LambdaExpressionNode) { var fnCompare = (LambdaExpressionNode)arg; if (fnCompare.ArgumentNames.Length != 2) { throw new ArgumentException("compare function must accept 2 arguments"); } comparer = new LambdaComparer(fnCompare); } else if (arg is Delegate) { comparer = new DelegateComparer((Delegate)arg); } AssertUtils.ArgumentNotNull(comparer, "comparer", "orderBy(comparer) argument 'comparer' does not evaluate to a supported type"); var list = new ArrayList(source); list.Sort(comparer); return(list); }
/// <summary> /// Sorts the source collection using custom sort criteria. /// </summary> /// <remarks> /// Please note that your compare function needs to take care about /// proper conversion of types to be comparable! /// </remarks> /// <param name="source"> /// The source collection to sort. /// </param> /// <param name="args"> /// Sort criteria to use. /// </param> /// <returns> /// A sorted array containing collection elements. /// </returns> public object Execute(ICollection source, object[] args) { if (source == null || source.Count == 0) { return source; } if (args == null || args.Length != 1) { throw new ArgumentException("compare expression is a required argument for orderBy"); } var arg = args[0]; IComparer comparer = null; if (arg is string) { var expCompare = Expression.Parse((string) arg); comparer = new SimpleExpressionComparer(expCompare); } else if (arg is IComparer) { comparer = (IComparer) arg; } else if (arg is LambdaExpressionNode) { var fnCompare = (LambdaExpressionNode) arg; if (fnCompare.ArgumentNames.Length != 2) { throw new ArgumentException("compare function must accept 2 arguments"); } comparer = new LambdaComparer(fnCompare); } else if (arg is Delegate) { comparer = new DelegateComparer((Delegate) arg); } AssertUtils.ArgumentNotNull(comparer, "comparer", "orderBy(comparer) argument 'comparer' does not evaluate to a supported type"); var list = new ArrayList(source); list.Sort(comparer); return list; }