2 回答
TA贡献1844条经验 获得超8个赞
您可以使用Skip并Take实现它。
var input = new[] { "a", "b", "c", "d", "e" };
var res = input.Take(1)
.Concat(input.Skip(1).Take(3).OrderByDescending(e => e))
.Concat(input.Skip(4));
你也可以像这样做一个扩展方法
public static class IEnumerableExt
{
public static IEnumerable<TSource> OrderRangeByDescending<TSource, TKey>(this IEnumerable<TSource> input, Func<TSource, TKey> keySelector, int from, int length)
{
return input.Take(from)
.Concat(input.Skip(from).Take(length).OrderByDescending(keySelector))
.Concat(input.Skip(from + length));
}
public static IEnumerable<TSource> OrderRangeBy<TSource, TKey>(this IEnumerable<TSource> input, Func<TSource, TKey> keySelector, int from, int length)
{
return input.Take(from)
.Concat(input.Skip(from).Take(length).OrderBy(keySelector))
.Concat(input.Skip(from + length));
}
}
var input = new[] { "a", "b", "c", "d", "e" };
var res = input.OrderRangeByDescending(e => e, 1, 3);
TA贡献1856条经验 获得超11个赞
重复调用Skip和Take可能会对性能产生影响,特别是如果源是由密集计算生成的。最佳解决方案将要求对源的读取仅发生一次。这可以通过将源拆分为多个枚举但使用单个枚举器来实现。拥有这样一个 Splitter 将使我们能够很容易地实现OrderRangeBy/OrderRangeByDescending方法:
public static IEnumerable<TSource> OrderRangeBy<TSource, TKey>(
this IEnumerable<TSource> source, Func<TSource, TKey> keySelector,
int startIndex, int endIndexExclusive)
{
var parts = source.Split(startIndex, endIndexExclusive);
return parts[0].Concat(parts[1].OrderBy(keySelector)).Concat(parts[2]);
}
public static IEnumerable<TSource> OrderRangeByDescending<TSource, TKey>(
this IEnumerable<TSource> source, Func<TSource, TKey> keySelector,
int startIndex, int endIndexExclusive)
{
var parts = source.Split(startIndex, endIndexExclusive);
return parts[0].Concat(parts[1].OrderByDescending(keySelector)).Concat(parts[2]);
}
下面是 Splitter 的一个实现:
public static IEnumerable<TSource>[] Split<TSource>(
this IEnumerable<TSource> source, params int[] indices)
{
var parts = new IEnumerable<TSource>[indices.Length + 1];
var enumerator = source.GetEnumerator();
int index = 0;
for (int i = 0; i < indices.Length; i++)
{
parts[i] = GetPart(indices[i]);
}
parts[indices.Length] = GetPart(Int32.MaxValue);
return parts;
IEnumerable<TSource> GetPart(int maxIndexExclusive)
{
if (index >= maxIndexExclusive) goto finish;
while (enumerator.MoveNext())
{
yield return enumerator.Current;
index++;
if (index >= maxIndexExclusive) break;
}
finish: if (maxIndexExclusive == Int32.MaxValue) enumerator.Dispose();
}
}
- 2 回答
- 0 关注
- 119 浏览
添加回答
举报
