reordering array of different data types ?

Hi,

I have different types of arrays let’s name them classA(), classB(), classC() and so on wtih same bounds (5) and needs
to reorder those like that:
0,2,3,4,1,5

At the moment , I need for all classes a seperate methoad to get right data type. When using a variant array, I need to determine all classes,
make a new variant array, copy it t varient , reorder and copy it back to the correct correct data type which seems not efficient and long coding.
I know there’s a sortWith method but this work only ascending. Is there an efficient way of reordering array of different type with specified indexes ?

Currently I do this but have a own method for each class:

[code]
dim shiftIdx as integer = if(indexToRemove>indexToInsert, 1, 0 )

if indexToInsert>theArray.Ubound then
theArray.Append theArray(indexToRemove)
theArray.Remove(indexToRemove)
else
theArray.Insert(indexToInsert, theArray(indexToRemove) )
theArray.Remove(indexToRemove+shiftIdx )
end if[/code]

Sortwith can be used for just about anything. For instance you have an array of 6 objects (0-5). If you do

Dim Index() As Integer = Array(5,4,3,2,1,0) Index.SortWith(theObject)

You will get a descending sort.

Dim Index() As Integer = Array(0,4,1,2,3,5) Index.SortWith(theObject)

Will result in an array (0,2,3,4,1,5)

[quote=211674:@Wayne Golding]Sortwith can be used for just about anything. For instance you have an array of 6 objects (0-5). If you do

Dim Index() As Integer = Array(5,4,3,2,1,0) Index.SortWith(theObject)

You will get a descending sort.

Dim Index() As Integer = Array(0,4,1,2,3,5) Index.SortWith(theObject)

Will result in an array (0,2,3,4,1,5)[/quote]

Not sure if I don’t understand but Index is always sorted descending. Also for the 2nd example.

You can use SortWith to create the array that’ll sort in the order you want.

To remap an array into this target ordering

dim targetOrder() As Integer = Array(0,2,3,4,1,5)

first make a linear array

dim sorter() As Integer = Array(0,1,2,3,4,5)

and sort it by targetOrder

targetOrder.SortWith(sorter)

Now sorter = Array(0,4,1,2,3,5). Use this to sort your actual array of classes

sorter.SortWith(classA, classB, classC)

To have the sorted result run the other way you can either specify your targetOrder in the other direction

dim targetOrder() As Integer = Array(5,1,4,3,2,0)

or initialize sorter in the other direction

dim sorter() As Integer = Array(5,4,3,2,1,0)

Doing either of those gives the output ordering (5,1,4,3,2,0). Doing both of them gives the original ordering of (0,2,3,4,1,5).

[code]Function createSorter(targetOrder() As Integer, ascending As boolean = true) As Integer()

dim last As integer = targetOrder.Ubound

dim sorter() As Integer
redim sorter(last)

if ascending then
for i As integer = 0 to last
sorter(i) = i
next
else
for i As integer = 0 to last
sorter(i) = last - i
next
end

//this sorts targetOrder too. Use a copy if not wanting to modify.
targetOrder.SortWith(sorter)

return sorter

End Function

dim targetOrder() As Integer = Array(0,2,3,4,1,5)

dim sorter() As Integer = createSorter(targetOrder, false)

sorter.SortWith(classA, classB, classC)
[/code]

(not really tested, my logic may be off :slight_smile: )

Thank Will for clearing it up.