TArrayHelper is in generics.collections.....Jamie...
All you need for sort and binarysearch is to provide a comparer for your record type.
Simple example:
{$mode delphi}
uses
classes,generics.defaults, generics.collections;
type
TARecord = record
min,
max:integer;
end;
TMinComparer = class(TInterfacedObject,IComparer<TARecord>)
function Compare(const Left, Right: TARecord): Integer; overload;
end;
TMaxComparer = class(TInterfacedObject,IComparer<TARecord>)
function Compare(const Left, Right: TARecord): Integer; overload;
end;
function TMinComparer.Compare(const left,right:TARecord):integer;
begin
result := 0;
if right.min > left.min then result := -1
else if right.min < left.min then result := 1;
end;
function TMaxComparer.Compare(const left,right:TARecord):integer;
begin
result := 0;
if right.max > left.max then result := -1
else if right.max < left.max then result := 1;
end;
var
RecordArray:TArray<TARecord> = nil; // same as array of TArecord.
MinComparer, MaxComparer:IComparer<TARecord>;
R:TARecord;
begin
MinComparer := TMinComparer.Create; // note: is IComparer interface.
MaxComparer := TMaxComparer.Create;
SetLength(RecordArray,4);
RecordArray[0].min := 100;
RecordArray[0].max := 200;;
RecordArray[1].min := 10;
RecordArray[1].max := 20;;
RecordArray[2].min := 500;
RecordArray[2].max := 2;
RecordArray[3].min := 7;
RecordArray[3].max := 17;
TArrayHelper<TARecord>.Sort(RecordArray,MinComparer);
for R in RecordArray do write(R.Min:4);
writeln;
TArrayHelper<TARecord>.Sort(RecordArray,MaxComparer);
for R in RecordArray do write(R.Max:4);
writeln;
end.
The BinarySearch works the same. Sort and BinarySearch are class methods, so TArrayHelper needs no instance.
The comparers are interfaces, so need no free.
You can use the same comparers for both.