Michael
Michael

Reputation: 175

Does List<>.OrderBy() check if the list is sorted?

In a game I am developing I need to sort a list of objects in order to determine in which order they are to be rendered. I need to to sort the list each frame, which is 60 times per second. However it is likely the list will already be in order, except on the first frame of the game after start.

So my question is whether the OrderBy method of List<> checks if the list is already ordered before using whatever algorithm it uses to sort? Since most of the time my list will be sorted it would be quite a waste of time to resort it anyway.

Upvotes: 2

Views: 1391

Answers (1)

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101681

It's not List<T>.OrderBy, it's Enumerable.OrderBy and no it doesn't check if the list is already sorted. You can see the source code here, if you wanna keep your list sorted all the time I suggest you to use SortedList or SortedList<TKey,TValue> instead.

Upvotes: 2

Related Questions