Jens Mühlenhoff
Jens Mühlenhoff

Reputation: 14873

How do I create a case insensitive list of string?

I'm trying to create a list of string that is case-insensitive.

The CreateList method let's me pass in some overloads:

I tried to use TStringComparer.OrdinalIgnoreCase like this:

var
  List: IList<string>;
begin
  List := TCollections.CreateList<string>(TStringComparer.OrdinalIgnoreCase);
end;

But since this comparer doesn't implement any of the above classes / interfaces that doesn't compile; I get:

E2250 There is no overloaded version of TCollections.CreateList<System.string> that can be called with these arguments

Is there an implementation of one of those available in the spring4d framework?

Upvotes: 2

Views: 789

Answers (2)

Stefan Glienke
Stefan Glienke

Reputation: 21748

You need to write the parentheses:

var
  List: IList<string>;
begin
  List := TCollections.CreateList<string>(TStringComparer.OrdinalIgnoreCase());
end;

Later compiler versions can figure it out without.

Upvotes: 4

Jens M&#252;hlenhoff
Jens M&#252;hlenhoff

Reputation: 14873

After closer inspection of the type TComparison:

type
  TComparison<T> = reference to function(const Left, Right: T): Integer;

The answer turns out to be quite tivial:

var
  List: IList<string>;
begin
  List := TCollections.CreateList<string>(AnsiCompareText);
end;

Upvotes: 1

Related Questions