Re: IInterfaceList and Sort

From: John Elrick (jelrick_at_adelphia.net)
Date: 10/28/03


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



Relevant Pages

  • Re: implementing roles in OOP......
    ... everything should be driven by abstracting the problem ... my draft interfaces define what sort of thing I would ... All I need is the Composite relationships to navigate, ... IEnumerator is an interface defined by my environment, we can put, ...
    (comp.object)
  • Re: LW9 running FIXED. hub problem found (i think)
    ... Well, this was all good news, sort of LOL ... I know its capable of some REALLY cool imagery, but why such an utterly goddam amateur interface? ... As for this part, I had to bust out laughing, as this was just too funny LOL ... I was able to start creating cool and FUN scenes pretty much right out of the shoot! ...
    (comp.graphics.apps.lightwave)
  • Re: Nemethis Tablelist widget vs. Hobbs Tktable widget
    ... > An Cobol man would say - if I must be quickly I sort using ... >> Csaba's Tablelist widget, and I'm looking for some feedback from ... concentrate more on interface specification then interface users can ... encapsulates a certain functionality. ...
    (comp.lang.tcl)
  • Re: How many use CorelDraw
    ... > You can download a full trial version to try - or borrow a ... What sort of interface does it offer for OpenType font features? ...
    (comp.publish.prepress)