Re: IInterfaceList and Sort
From: John Elrick (jelrick_at_adelphia.net)
Date: 10/28/03
- Next message: Dion Oliphant: "Re: Those little dots"
- Previous message: Ian Kirk: "Re: Those little dots"
- In reply to: Harley Pebley: "Re: IInterfaceList and Sort"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 28 Oct 2003 08:59:32 -0500
"Harley Pebley" <harley_pebley@idahotech.com> wrote in message
news:1n5r48nl1akd7$.3owk6fzxcgb.dlg@40tude.net...
> > I generate a collection of Interfaces and add them to a
> > IInterfaceList. Unfortunately there is no Sort method.
> >
> > What is the easiest way to sort such a collection?
>
> Create one:
> ISortedInterfaceList = interface(IInterfaceList)
> procedure Sort;
> end;
A more generic approach:
TCompareResult= (crLessThan, crEqual, crGreaterThan);
{ descend from this interface for specific uses
Example:
IIntegerComparable = interface(IComparable)
function asInteger: integer;
end;
TIntegerComparable = class(TInterfacedObject, IIntegerComparable,
IComparable)
function compare(const aComparator: IComparable): TCompareResult;
function asInteger: integer;
end;
function TIntegerComparable.compare(const aComparator: IComparable):
TCompareResult;
var
localCompare: integer;
begin
localCompare := (aComparator as IIntegerComparable).asInteger;
if asInteger = localCompare then
result := crEqual
else if asInteger < localCompare then
result := crLessThan
else
result := crGreaterThan;
end;
}
IComparable = interface
function compare(const aComparator: IComparable): TCompareResult;
end;
ISortable = interface
function get_Item(const aIndex: integer): IComparable;
function Count: integer;
procedure Exchange(const aFirstIndex, aSecondIndex: integer);
end;
ISorter = interface
procedure Sort(const aSortable: ISortable);
end;
TQuickSort = class(TInterfacedObject, ISorter)
procedure Sort(const aSortable: ISortable);
end;
TTreeSort = class(TInterfacedObject, ISorter)
procedure Sort(const aSortable: ISortable);
end;
{ Hey, why not<g> }
TBubbleSort = class(TInterfacedObject, ISorter)
procedure Sort(const aSortable: ISortable);
end;
ISortedInterfaceList = interface
procedure Sort;
end;
> TSortedInterfaceList = class(TInterfaceList, IInterfaceList, ISortable,
ISortedList)
> procedure Sort;
function get_Item(const aIndex: integer): IComparable;
procedure Exchange(const aFirstIndex, aSecondIndex: integer);
> end;
>
procedure TSortedInterfaceList.Sort;
var
localSorter : ISorter;
begin
localSorter := TQuickSort.Create;
localSorter.Sort(Self);
end;
TQuickSort.Sort can be modified from TList. The nice part is that once it's
written to take IComparable and ISortable, the sort routine will work with
_any_ implementor.
FWIW
John
- Next message: Dion Oliphant: "Re: Those little dots"
- Previous message: Ian Kirk: "Re: Those little dots"
- In reply to: Harley Pebley: "Re: IInterfaceList and Sort"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|