lundi 28 septembre 2015

Are we allowed to OrderBy(Random)?

I sometimes shuffle lists or arrays using OrderBy (item => R.NextDouble()), where Random R is initialized elsewhere.

Now this clearly is a hack, though the one recommended everywhere on this site, and it does work very nicely in practice.

However, it was also pointed out that this makes the assumption that the sorting algorithm does not get confused by changing values of a single item and e.g. enters an infinite loop.

My question is if there is some sort of implicit or explicit guarantee that this won't happen. I could not find anything about it in MSDN.

While there are very good, specialized O(n) algorithms for this purpose, I don't always want to search and copy-paste them in little side projects (we are not talking about prod here). They would obviously be the right solution.

Also note that performance is not an issue at all.




Aucun commentaire:

Enregistrer un commentaire