当前位置: 首页>>代码示例>>C#>>正文


C# RubyArray.Sort方法代码示例

本文整理汇总了C#中RubyArray.Sort方法的典型用法代码示例。如果您正苦于以下问题:C# RubyArray.Sort方法的具体用法?C# RubyArray.Sort怎么用?C# RubyArray.Sort使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在RubyArray的用法示例。


在下文中一共展示了RubyArray.Sort方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SortInPlace

        internal static RubyArray SortInPlace(ComparisonStorage/*!*/ comparisonStorage, BlockParam block, RubyArray/*!*/ self, out StrongBox<object> breakResult) {
            breakResult = null;
            var context = comparisonStorage.Context;

            // TODO: this does more comparisons (and in a different order) than
            // Ruby's sort. Also, control flow won't work because List<T>.Sort wraps
            // exceptions from the comparer & rethrows. We need to rewrite a version of quicksort
            // that behaves like Ruby's sort.
            if (block == null) {
                self.Sort((x, y) => Protocols.Compare(comparisonStorage, x, y));
            } else {
                object nonRefBreakResult = null;
                try {
                    self.Sort((x, y) =>
                    {
                        object result = null;
                        if (block.Yield(x, y, out result)) {
                            nonRefBreakResult = result;
                            throw new BreakException();
                        }

                        if (result == null) {
                            throw RubyExceptions.MakeComparisonError(context, x, y);
                        }

                        return Protocols.ConvertCompareResult(comparisonStorage, result);
                    });
                } catch (InvalidOperationException e) {
                    if (e.InnerException == null) {
                        throw;
                    }

                    if (e.InnerException is BreakException) {
                        breakResult = new StrongBox<object>(nonRefBreakResult);
                        return null;
                    } else {
                        throw e.InnerException;
                    }
                }
            }

            return self;
        }
开发者ID:Jirapong,项目名称:main,代码行数:43,代码来源:ArrayOps.cs

示例2: SortInPlace

        public static RubyArray/*!*/ SortInPlace(
            BinaryOpStorage/*!*/ comparisonStorage,
            BinaryOpStorage/*!*/ lessThanStorage,
            BinaryOpStorage/*!*/ greaterThanStorage,            
            BlockParam block, RubyArray/*!*/ self) {

            var context = comparisonStorage.Context;
            RubyUtils.RequiresNotFrozen(context, self);

            // TODO: this does more comparisons (and in a different order) than
            // Ruby's sort. Also, control flow won't work because List<T>.Sort wraps
            // exceptions from the comparer & rethrows. We need to rewrite a version of quicksort
            // that behaves like Ruby's sort.
            if (block == null) {
                self.Sort(delegate(object x, object y) {
                    return Protocols.Compare(comparisonStorage, lessThanStorage, greaterThanStorage, x, y);
                });
            } else {
                try {
                    self.Sort(delegate(object x, object y) {
                        object result;
                        if (block.Yield(x, y, out result)) {
                            // TODO: this doesn't work
                            throw new BreakException();
                        }

                        if (result == null) {
                            throw RubyExceptions.MakeComparisonError(context, x, y);
                        }

                        return Protocols.ConvertCompareResult(lessThanStorage, greaterThanStorage, result);
                    });
                } catch (BreakException) {
                }
            }

            return self;
        }
开发者ID:toddb,项目名称:ironruby,代码行数:38,代码来源:ArrayOps.cs

示例3: RubyArray_Misc

        public void RubyArray_Misc() {
            RubyArray a;

            a = new RubyArray(new[] { 3, 5, 2, 4, 1 });
            a.InsertRange(0, new[] {10, 20});
            a.RemoveRange(0, 2);

            a.Sort();
            Assert(ArrayUtils.ValueEquals(a.ToArray(), new object[] { 1, 2, 3, 4, 5 }));
            a.Sort((x, y) => (int)x == (int)y ? 0 : ((int)x < (int)y ? 1 : -1));
            Assert(ArrayUtils.ValueEquals(a.ToArray(), new object[] { 5, 4, 3, 2, 1 }));
            a.Reverse();
            Assert(ArrayUtils.ValueEquals(a.ToArray(), new object[] { 1, 2, 3, 4, 5 }));

            a.Freeze();
            AssertExceptionThrown<RuntimeError>(() => a.Reverse());
            AssertExceptionThrown<RuntimeError>(() => a.Sort());
        }
开发者ID:rpattabi,项目名称:ironruby,代码行数:18,代码来源:RubyArrayTests.cs


注:本文中的RubyArray.Sort方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。