My objective is: Given a list of entries, and a desired ordering, rearrange the list of entries according to this ordering. The list will be very large, so space efficiency is important.
Ex:
List<Entry> data = ReadDataFromSomeWhere(); // data => [a, b, c];
List<int> ordering = RandomPermutation(data.Count); // ordering => [2, 1, 3];
data.ReOrderBy(ordering); // data => [b, a, c];
I may be mistaken, but it seems like the most straightforward and space efficient solution is to sort/orderby the data by the ordering. or more generally:
Given two lists: A,B is there a way to sort A by B? The functionality would be essentially the same as:
Array.Sort<(Of <(TKey, TValue>)>)(array<TKey>[]()[], array<TValue>[]()[])
One methodology that comes to mind is to create a new datatype which is composed from A and B, ie. Pair, then sort by the B values:
List<T> A;
List<T> B;
Assert(A.Count == B.Count);
var C = A.Select( (a,idx) => new Pair<T,T>(B[idx],a)).OrderBy(c => c.First);
A = C.Select(x => x.Second).ToList();
However, I would like this to be as space efficient as possible (both select's and the tolist() call I'm guessing are expensive), so a largely in-place sort is necessary. To this end, is there a way to write a comparer for A.Sort(), which references B?