Task[edit]
Find the index of a string (needle) in an indexable, ordered collection of strings (haystack).

Raise an exception if the needle is missing.

If there is more than one occurrence then return the smallest index to the needle.

Extra credit
Return the largest index to a needle that has multiple occurrences in the haystack.

See also
 Search a list of records

program Needle;
 
{$APPTYPE CONSOLE}
 
uses
  SysUtils, Classes;
 
var
  list: TStringList;
  needle: string;
  ind: Integer;
begin
  list := TStringList.Create;
  try
    list.Append('triangle');
    list.Append('fork');
    list.Append('limit');
    list.Append('baby');
    list.Append('needle');
 
    list.Sort;
 
    needle := 'needle';
    ind := list.IndexOf(needle);
    if ind < 0 then
      raise Exception.Create('Needle not found')
    else begin
      Writeln(ind);
      Writeln(list[ind]);
    end;
 
    Readln;
  finally
    list.Free;
  end;
end.