* Delphi compatible TCollection.Sort() using helper

This commit is contained in:
Michaël Van Canneyt 2024-02-21 10:31:24 +01:00
parent 5c890b59e3
commit 97495e93b8
3 changed files with 40 additions and 0 deletions

View File

@ -1089,6 +1089,13 @@ function _LookupVtableInfo(AGInterface: TDefaultGenericInterface; ATypeInfo: PTy
function _LookupVtableInfoEx(AGInterface: TDefaultGenericInterface; ATypeInfo: PTypeInfo; ASize: SizeInt;
AFactory: THashFactoryClass): Pointer;
Type
TCollectionItemComparer = IComparer<TCollectionItem>;
TCollectionHelper = Class helper for TCollection
Procedure sort(const AComparer: TCollectionItemComparer); overload;
end;
implementation
{ TComparer<T> }
@ -3481,5 +3488,25 @@ begin
end;
end;
{ TCollectionHelper }
Function GenericCollSort(Item1,Item2 : TCollectionItem; aContext : Pointer) : Integer;
begin
Result:=TCollectionItemComparer(aContext).Compare(Item1,Item2);
end;
Procedure TCollectionHelper.sort(const AComparer: TCollectionItemComparer);
begin
aComparer._AddRef;
try
Sort(GenericCollSort,Pointer(aComparer));
finally
aComparer._Release;
end;
end;
end.

View File

@ -804,6 +804,7 @@ type
TCollectionItemClass = class of TCollectionItem;
TCollectionNotification = (cnAdded, cnExtracting, cnDeleting);
TCollectionSortCompare = function (Item1, Item2: TCollectionItem): Integer;
TCollectionSortCompare_Context = function (Item1, Item2: TCollectionItem; context : Pointer): Integer;
TCollection = class(TPersistent)
private
@ -848,6 +849,7 @@ type
procedure Exchange(Const Index1, index2: integer);
procedure Move(Const Index1, index2: integer);
procedure Sort(Const Compare : TCollectionSortCompare);
procedure Sort(Const Compare : TCollectionSortCompare_Context; Context : Pointer);
property Count: Integer read GetCount;
property ItemClass: TCollectionItemClass read FItemClass;
property Items[Index: Integer]: TCollectionItem read GetItem write SetItem;

View File

@ -416,6 +416,17 @@ begin
end;
end;
procedure TCollection.Sort(Const Compare : TCollectionSortCompare_Context; Context : Pointer);
begin
BeginUpdate;
try
FItems.Sort(TListSortComparer_Context(Compare),Context);
Finally
EndUpdate;
end;
end;
procedure TCollection.Sort(Const Compare : TCollectionSortCompare);
begin