Created
September 20, 2020 23:34
-
-
Save sancarn/a3baeb1b69f02ff75577a9c1dee0c175 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Public Function Sort(cb as callback, ctor as object) as stdArray 'stdArray? Or can i use stdEnum again? and have a `stdArray.Create(stdEnum)`? | |
Dim arr() as SortStruct | |
Redim arr(1 to me.count) | |
Dim iIndex as Long: iIndex = 0 | |
Dim iFirstItem as long: iFirstItem = 1 | |
Dim val as variant | |
For each val in pEnumObject | |
'Increment index | |
iIndex = iIndex + 1 | |
'Bind to SortStruct | |
if iIndex > 1 Then | |
'Initialise sorting struct | |
Call CopyVariant(arr(iIndex).value, val) | |
arr(iIndex).iIndex = iIndex | |
arr(iIndex).sortValue = cb.Run(arr(iIndex).value) | |
'Sort/Compare | |
Dim iCompareIndex as long: iCompareIndex = iFirstItem | |
Do While iCompareIndex <> 0 | |
'If sort value at current index is less than at compare index then but this index to compare index via next | |
if arr(iIndex).sortValue < arr(iCompareIndex).sortValue then | |
'Bind this index to compare index via iNext property | |
arr(iIndex).iNext = arr(iCompareIndex).iIndex | |
'Rebind previous element if required | |
if arr(iCompareIndex).iPrev <> 0 then | |
'There is a previous element, rebind it! | |
arr(arr(iCompareIndex).iPrev).iNext = iIndex | |
else | |
'There is no previous element i.e. this is the first element, change iFirstItem | |
iFirstItem = iIndex | |
End if | |
'No need to carry on searching for where item should go, exit do loop | |
Exit Do | |
Else | |
'Ensure next element defined, if not then we have a new next element | |
if arr(iCompareIndex).iNext <> 0 then | |
'Schedule next sorting check and keep searching | |
iCompareIndex = arr(iCompareIndex).iNext | |
else | |
'Next element is not defined, therefore this is max | |
'in this case set next of arr(iCompareIndex) to this | |
'set prev of this to iCompareIndex | |
arr(iCompareIndex).iNext = iIndex | |
arr(iIndex).iPrev = iCompareIndex | |
'No need to carry on searching for where item should go, exit do loop | |
Exit Do | |
end if | |
end if | |
Loop | |
Else | |
'Initialise sorting struct | |
Call CopyVariant(arr(1).value, val) | |
arr(1).sortValue = cb.Run(arr(1).value) | |
arr(1).iIndex = 1 | |
arr(1).iNext = 0 | |
arr(1).iPrev = 0 | |
end if | |
next | |
'Collect sorted elements | |
Dim ret as stdArray | |
Dim i as long: i = iFirstItem | |
While i <> 0 | |
Call ret.push(arr(i).value) | |
i = arr(i).iNext | |
Wend | |
'Return sorted array | |
set sort = ret | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment