如何随机订购 IEnumerable<>?
我有这个 IEnumerable :
IEnumerable<MyObject>
我需要随机排序 MyObject 的列表。我需要转换成 ArrayList 吗?
或者我可以直接做吗?谢谢
编辑
这是我实际的随机排序函数:
IList<ArchiePacchettoOfferta> list = new List<ArchiePacchettoOfferta>(m_oEnum);
for (int i = 0; i < list.Count; ++i)
{
HttpContext.Current.Response.Write(list[i].Titolo + "<br />");
}
Random rnd = new Random();
for (int i = 0; i < list.Count; ++i)
{
int swapIndex = rnd.Next(i + 1);
ArchiePacchettoOfferta tmp = list[i];
list[i] = list[swapIndex];
list[swapIndex] = tmp;
}
for (int i = 0; i < list.Count; ++i)
{
HttpContext.Current.Response.Write(list[i].Titolo + "<br />");
}
它每次都以相同的方式对列表进行排序:(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ToList 仅用于确保在多次迭代洗牌时返回相同(随机)的顺序。当然,如果您不再需要原始数据,您可以“就地”进行洗牌。
更新
有一些关于是否应该依赖 OrderBy 只比较元素一次的讨论。如果您不想相信您的 .NET 实现能够做到这一点,请详细说明:
请参阅这些链接以获取有关此解决方案的潜在问题的更多背景信息(主要是当前示例的性能下降,但也存在非-均匀分布):
The ToList is only there to ensure that the same (random) order is returned when iterating over shuffled more than once. Of course you can shuffle 'inplace' if you don't need the original anymore
Update
There is some discussion on whether you should rely on OrderBy comparing elements only once. If you don't want to do trust your .NET implementation to do that, spell it out:
See these links for more background on what potential problem this solves (mainly a performance degradation for the current sample, but also the risk of having non-uniform distribution):
枚举器仅允许“迭代”访问。因此,不可能随机或以排序方式访问元素。您必须读取枚举值并将它们(暂时)存储在另一个列表中,您可以对其应用(随机)排序算法。
[编辑] 示例:(
我尚未测试示例代码。)
Enumerators allow "iterated" access only. Thus, there is no possibility to access the elements randomly or in a sorted way. You have to read the enumerated values and (temporarily) store them in another list for which you can apply your (random) sorting algorithm.
[edit] Example:
(I have not tested the example code.)
您不能将
IEnumerable
强制转换为其他类型 - 并且您也不想转换为非泛型无论如何,ArrayList
类型。但是,您可以做的是将所有内容读入列表(使用
ToList
扩展方法最简单),然后对其进行洗牌。 我的回答有一个示例洗牌代码——诚然是在迭代器块内,但内容就在那里。编辑:如果您每次都看到相同的顺序,我怀疑您在短时间内创建了
Random
的多个实例,并且它们都具有相同的种子。请阅读本文了解详细信息和解决方法。
You can't just cast an
IEnumerable<T>
to a different type - and you wouldn't want to convert to the non-genericArrayList
type anyway.However, what you can do is read everything into a list (simplest with the
ToList
extension method) and then shuffle that. My answer here has an example of the shuffle code - admittedly within an iterator block, but the guts are there.EDIT: If you're seeing the same ordering every time, I suspect you're creating multiple instances of
Random
in a short space of time, and they've all got the same seed.Read this article for details and workarounds.