TOCIForm class procedure QuickSortStringsObjects : LUSENET : OCI Best Practices : One Thread

There is a new method in TOCIForm. Use it to sort a TStrings object using a compare function you provide. You might use this, for example, if you have a tab control where the Tabs TStrings property stores collection item dates and a reference to the collection item. You would call the QuickSortStringsObjects method passing the tab control Tabs property. QuickSortStringsObjects runs a quick sort, calling your compare routine. Your compare routine simply compares values from the two passed pointers (i.e., two TObject references from your tab control) and returns a negative value, a positive value, or zero depending on the sort order of the items.

Here's a simple example where a list box Items property stores dates and TDateTimePicker object references. To sort the list box in date order you'd code:

function CompareDates(Item1, Item2: Pointer): Integer;
  if (TDateTimePicker(Item1).Date < TDateTimePicker(Item2).Date)
    then Result := -1
  else if (TDateTimePicker(Item1).Date = TDateTimePicker(Item2).Date)
    then Result := 0
  else Result := 1;

procedure TMyOCIForm.Button1Click(Sender: TObject);
  QuickSortStringsObjects(ListBox1.Items, CompareDates);

-- Anonymous, August 26, 1998

Moderation questions? read the FAQ