While I'm pretty certain that the keys are unordered when returned from linq's ToLookup method, Is the order of values preserved? I can't find any documentation that says one way or the other.
+1
A:
The current implementation of ToLookup()
does indeed preserve the order of the values (check the implementation of Lookup<TKey,TElement>.Grouping<TKey,TElement>.Add()
), but I do not believe it is guaranteed to stay that way. To guarantee ordering, your best bet is probably to include the element's original index, perhaps using Select
's with-index overload, and then sort again.
dahlbyk
2009-07-27 19:13:00