Preserve order of values with linq and ToLookup()
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.
Yes. According to the documentation:
The values within each group are in the same order as in
source
.