_ArraySortIndirect

I was looking for a way to implement a good sort algorithm that didn't actually
move the data in my arrays around. Instead, I wanted a 2nd array, or even a 2nd
*column* to be used for that purpose. That's how _ArraySortIndirect came about.
While looking through my Algorithms book, I figured 'QuickSort' was a good idea
to implement - but how about that, _ArraySort() already uses QuickSort, and even
optimizes to use InsertionSort when necessary. I was pretty happy about that,
since I'd have very little to do to implement what I had in mind.

In any
case, here's what's needed:1 array of indexes1 array of data to be
'indirectly' sorted

Simplest way to do this is to 'Dim' a 2nd array to be
the size of the array-to-sort, then let this function do an 'initialize indexes'
routine to set the indexes to be n-to-z. After the function call, the indexes
array will be sorted, the data array will be untouched.

Now, to access
the data in the data array, you'll need to do a double-lookup, as in
$aDataArray[$aIndexArray[$i]], but that can be a bit more convenient a possibly
time-saving than resorting an entire 2Dimensional array. (it's slower on
one-dimensional arrays, but not by a big margin)

The second option would
be an in-array 2D indirect sort. This means one column is the 'index' column,
and the rest are Data columns. Accessing it would be the same, except a second
subscript would be needed for the Data columns. This method of sorting is slower
than indirect sorting, but saves you an extra array. Its up to you what you feel
like using.

Ascend4nt's AutoIT Code License agreement:While I provide this source code freely, if you do use the code in your projects, all I ask is that:

If you provide source, keep the header as I have put it, OR, if you
expand it, then at least acknowledge me as the original author, and any other authors I credit

If the program is released, acknowledge me in your credits (it
doesn't have to state which functions came from me, though again if
the source is provided - see #1)

The source on it's own (as opposed to part of a project) can not be posted unless a link to the page(s) where the code were retrieved from is provided and a message stating that the latest updates will be available on the page(s) linked to.

Pieces of the code can however be discussed on the threads where
Ascend4nt has posted the code without worrying about further linking.

For $i=1 To 399 ; Set the 2D array's 1st column to show sorted indirect indexes $aRandomNumArray2D[$i][0]=$aSortedIndexes[$i] ; Set the 2D array's 4th column to reflect correct order $aRandomNumArray2D[$i][3]=$aRandomNumArray2D[$aSortedIndexes[$i]][2]Next

If Not IsArray($aArrayToSort) Or Not IsArray($aIndexArray) Or UBound($aIndexArray,0)>1 Then Return SetError(1,0,False)

If $iIndexEnd=-1 Then $iIndexEnd=UBound($aIndexArray)-1

If $iIndexStart<0 Or $iIndexStart>$iIndexEnd Or $iIndexEnd>UBound($aIndexArray)-1 Then Return SetError(1,0,False) If $iDataStart<0 Or $iDataStart+($iIndexEnd-$iIndexStart)>UBound($aArrayToSort)-1 Then Return SetError(1,0,False)

If $bInitializeIndexes Then Local $iDest=$iDataStart For $i=$iIndexStart To $iIndexEnd $aIndexArray[$i]=$iDest $iDest+=1 Next EndIf

Func _ArraySortIndexes(ByRef $aArrayToSort,$iDescending=0,$iStart=0,$iEnd=-1,$iIndexColumn=0,$iDataColumn=1,$bInitializeIndexes=True) If Not IsArray($aArrayToSort) Or UBound($aArrayToSort,0)<>2 Then Return SetError(1,0,False)

If $iEnd=-1 Then $iEnd=UBound($aArrayToSort)-1

If $iStart<0 Or $iStart>$iEnd Or $iEnd>UBound($aArrayToSort)-1 Then Return SetError(1,0,False)

If $iIndexColumn<0 Or $iIndexColumn>UBound($aArrayToSort,2)-1 Then Return SetError(1,0,False) If $iDataColumn=$iIndexColumn Or $iDataColumn<0 Or $iDataColumn>UBound($aArrayToSort,2)-1 Then Return SetError(1,0,False)

If $bInitializeIndexes Then For $i=$iStart To $iEnd $aArrayToSort[$i][$iIndexColumn]=$i Next EndIf