tags:

views:

165

answers:

1

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?

+3  A: 

You probably have Items.Sorted=True.

Lars D
You nailed it :) Items.Sorted was true, obviously resulting in some weird behaviour. I also updated my question to remove the code mistakes you pointed out. Can you please rephrase your answer to just point to the Sorted property? Then I'll accept it.
DR