[Overview][Constants][Types][Classes][Procedures and functions][Variables][Index] | Reference for unit 'Objects' (#rtl)

# TStrCollection.Compare

Compare two strings in the collection.

## Declaration

Source position: objects.pp line 470

`function TStrCollection.Compare(`
`Key1: Pointer;`
`Key2: Pointer`
`):Sw_Integer; virtual;`

## Description

TStrCollection overrides the Compare function so it compares the two keys as if they were pointers to strings. The compare is done case sensitive. It returns

-1
  if the first string is alphabetically earlier than the second string.
0
  if the two strings are equal.
1
  if the first string is alphabetically later than the second string.

## Errors

None.

## See also

TSortedCollection.Compare |  | Compare two items in the collection.

## Example

    Program ex38;

    { Program to demonstrate the TStrCollection.Compare method }

    Uses Objects,Strings;

    Var C : PStrCollection;
        S : String;
        I : longint;
        P : Pchar;

    begin
      Randomize;
      C:=New(PStrCollection,Init(120,10));
      C^.Duplicates:=True; { Duplicates allowed }
      Writeln ('Inserting 100 records at random places.');
      For I:=1 to 100 do
        begin
        Str(Random(100),S);
        S:='String with value '+S;
        P:=StrAlloc(Length(S)+1);
        C^.Insert(StrPCopy(P,S));
        end;
      For I:=0 to 98 do
        With C^ do
          If Compare (At(I),At(I+1))=0 then
            Writeln ('Duplicate string found at position ',I);
      Dispose(C,Done);
    end.

Documentation generated on: May 14 2021