Dear knowledgeable ones.
Some time ago I made a few of my routines kind of universal, and I wonder if there is any way in Delphi (My version is Delphi9) that can be used to reuse this code in example below.
As you see, the two procedures are almost identical, it is just the typedeclaration that is different in the parameters passed to the procedures.
Maybe it is possible to use some kind of typecasting but my experience there is limited.
The code is not bad if only 2 similar blocks, I just see that it would be better with one block of code that handles sorting of maybe 10 different types of data..
Best regards, and thanks in advance for input on this issue.
Example of code:
PROCEDURE BinarySortIndex(VAR ArrParam,ArrParamIndex: DynamicIntegerArray; CONST HighBound:INTEGER); OVERLOAD;
VAR
x : INTEGER;
TempVar : INTEGER;
BEGIN
Lo:=0; Hi:=HighBound; Mid:=FindMid;
TempVar := ArrParam[HighBound];
REPEAT
IF TempVar>ArrParam[ArrParamIndex[Mid]] THEN Lo:=Mid ELSE Hi:=Mid;
Mid:=FindMid;
UNTIL (Mid=Lo) OR (Mid=Hi);
IF TempVar>ArrParam[ArrParamIndex[Mid]] THEN INC(Mid);// We always need a last check just in case.
FOR x:=HighBound-1 DOWNTO Mid DO ArrParamIndex[x+1] := ArrParamIndex[x];// Shift the index.
ArrParamIndex[Mid]:=HighBound;// Store the pointer to index at its sorted place
END;
PROCEDURE BinarySortIndex(VAR ArrParam:DynamicStringArray; VAR ArrParamIndex: DynamicIntegerArray; CONST HighBound:INTEGER); OVERLOAD;
VAR
x : INTEGER;
TempVar : STRING;
BEGIN
Lo:=0; Hi:=HighBound; Mid:=FindMid;
TempVar := ArrParam[HighBound];
REPEAT
IF TempVar>ArrParam[ArrParamIndex[Mid]] THEN Lo:=Mid ELSE Hi:=Mid;
Mid:=FindMid;
UNTIL (Mid=Lo) OR (Mid=Hi);
IF TempVar>ArrParam[ArrParamIndex[Mid]] THEN INC(Mid);// We always need a last check just in case.
FOR x:=HighBound-1 DOWNTO Mid DO ArrParamIndex[x+1] := ArrParamIndex[x];// Shift the index.
ArrParamIndex[Mid]:=HighBound;// Store the pointer to index at its sorted place
END;