Daniel Rikowski
Daniel Rikowski

Reputation: 72544

Why is my TStringList not sorted?

I have a custom sorted TStringList...

Items.CustomSort(@CompareWords);

... with this comparison function:

function CompareWords(List: TStringList; Index1, Index2: Integer): Integer;
begin
  Result := StrIComp(PWideChar(List[Index1]), PWideChar(List[Index2]));
end;

But after noticing some problems with my code, which expects the list to be sorted in the order StrIComp induces, I created this small check...

for i := 1 to Items.Count - 1 do
begin
  Assert(StrIComp(PWideChar(Items[i-1]), PWideChar(Items[i])) <= 0);
end;

... and it fails.

Why isn't the list sorted properly?

Upvotes: 2

Views: 469

Answers (1)

Lars D
Lars D

Reputation: 8573

You probably have Items.Sorted=True.

Upvotes: 3

Related Questions