This is a new site that's currently running on alpha code. There are going to be bugs. If you discover any, please report them on the site's issues page (GitHub account required). Thanks.
Warning: Many URLs are going to change. Refer to the README file to discover which library project's documentation has been completed.
Project: Array Utilities Unit
Unit: DelphiDabbler.Lib.ArrayUtils
Record: TArrayUtils
Applies to: ~>0.1
class function Compare<T>(const ALeft, ARight: array of T;
const AComparer: TComparison<T>): Integer;
overload; static;
class function Compare<T>(const ALeft, ARight: array of T;
const AComparer: IComparer<T>): Integer;
overload; static;
class function Compare<T>(const ALeft, ARight: array of T): Integer;
overload; static;
Compares two arrays with elements of the same type. A value is returned that indicates the relationship of the two arrays.
Parameters:
ALeft - The first array to be compared.
ARight - The second array to be compared
AComparer - An optional function or object that can be used to compare two values. Used to compare the elements of the arrays.
If AComparer is provided it must be one of:
If this parameter is omitted then the default comparer defined by the TComparer<T>.Default method from the Delphi’s System.Generics.Defaults unit is used.
Returns:
An Integer value that should be interpreted as follows:
0
- The arrays are equal.Length(X) < Length(Y)
and the first Length(X)
elements of Y are equal to the elements of X then Y is greater than X.Using a comparer function:
procedure Compare_Eg1;
var
A, B, C, D, E: TArray<Integer>;
ComparerFn: TComparison<Integer>;
begin
A := TArray<Integer>.Create(1, 2, 3);
B := TArray<Integer>.Create(1, 2, 3);
C := TArray<Integer>.Create(1, 2);
D := TArray<Integer>.Create(1, 5, 7);
E := TArray<Integer>.Create(); // empty
ComparerFn := function(const Left, Right: Integer): Integer
begin
Result := Left - Right;
end;
Assert(TArrayUtils.Compare<Integer>(A, B, ComparerFn) = 0);
Assert(TArrayUtils.Compare<Integer>(A, C, ComparerFn) > 0);
Assert(TArrayUtils.Compare<Integer>(A, D, ComparerFn) < 0);
Assert(TArrayUtils.Compare<Integer>(A, E, ComparerFn) > 0);
end;
Using a comparer object:
procedure Compare_Eg2;
var
A, B, C, D, E: TArray<string>;
ComparerObj: IComparer<string>;
begin
A := TArray<string>.Create('a', 'b', 'c');
B := TArray<string>.Create('a', 'b', 'c');
C := TArray<string>.Create('a', 'b');
D := TArray<string>.Create('a', 'd', 'f');
E := TArray<string>.Create(); // empty
ComparerObj := TDelegatedComparer<string>.Create(CompareStr);
Assert(TArrayUtils.Compare<string>(A, B, ComparerObj) = 0);
Assert(TArrayUtils.Compare<string>(A, C, ComparerObj) > 0);
Assert(TArrayUtils.Compare<string>(A, D, ComparerObj) < 0);
Assert(TArrayUtils.Compare<string>(A, E, ComparerObj) > 0);
end;