在OrderBy中维护子序列顺序 [英] Maintain subsequence order in OrderBy

查看:72
本文介绍了在OrderBy中维护子序列顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个具有整数Day属性和对象IEnumerable<T>的类,其中对象的天数分别为2、3、4、1、3、3和5(按此顺序).

Suppose I have a class with an integer Day property and an IEnumerable<T> of objects where the days are 2, 3, 4, 1, 3, 3 and 5 (in that order).

是否有一种方法可以确保子元素的顺序(例如)从原始列表中的元素相对位置保持(例如)o.Day == 3而不需要的显式,自定义实现IEnumerable?

Is there a way to guarantee that the order of the subsequence where (for example) o.Day == 3 is maintained from its elements' relative positions in the original list without requiring an explicit, custom implementation of IEnumerable?

推荐答案

OrderBy被记录为;所以:您不需要做任何事情.

OrderBy is documented as being stable, if that is what you mean; so: you shouldn't need to do anything.

Enumerable.OrderBy

此方法执行稳定的排序;也就是说,如果两个元素的键相等,则保留元素的顺序.相反,不稳定排序不会保留具有相同键的元素的顺序.

This method performs a stable sort; that is, if the keys of two elements are equal, the order of the elements is preserved. In contrast, an unstable sort does not preserve the order of elements that have the same key.

这篇关于在OrderBy中维护子序列顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆