No matter how I try, I can't seem to create a nice and clean algorithm for doing the following:
- System.Array (or generic List) data of System.Object
- System.Array (or generic List) xval of System.Object
- System.Array (or generic List) idxs of System.Int32
xval and idxs contain the same number of elements and idxs contains no values less than zero or larger than data.Length
I need to insert all elements in xval into the data array, where the associated integer in idxs represents the insertion index in the original, unmolested data.
For example:
*data* = {A, B, C, D, E, F, G, H}
*xval* = {U, V, W, X, Y, Z}
*idxs* = {5, 0, 0, 0, 1, 1}
*output* = {V, W, X, A, Y, Z, B, C, D, E, U, F, G, H}
It's easy enough to do, but I always end up with icky code. My best attempt so far (but I'm worried about rounding errors):
idxs.Reverse()
xval.Reverse()
Dim N As Int32 = data.Count + xval.Count
Dim map(N - 1) As Double
Dim output(N - 1) As Object
Dim k As Int32 = -1
For i As Int32 = 0 To data.Count - 1
k += 1
map(k) = i
output(k) = data(i)
Next
For i As Int32 = 0 To xval.Count - 1
k += 1
map(k) = idxs(i) - ((i + 1) * 1e-8)
output(k) = xval(i)
Next
Array.Sort(map, output)