I have a simple TStringList. I do a TStringList.Sort on it.
Then I notice that the underscore “_” sorts before the capital letter “A”. This was in contrast to a third party package that was sorting the same text and sorted _ after A.
According to the ANSI character set, A-Z are characters 65 – 90 and _ is 95. So it looks like the 3rd party package is using that order and TStringList.Sort isn’t.
I drilled down into guts of TStringList.Sort and it is sorting using AnsiCompareStr (Case Sensitive) or AnsiCompareText (Case Insensitive). I tried it both ways, setting my StringList’s CaseSensitive value to true and then false. But in both cases, the “_” sorts first.
I just can’t imagine that this is a bug in TStringList. So there must be something else here that I am not seeing. What might that be?
What I really need to know is how can I get my TStringList to sort so that it is in the same order as the other package.
For reference, I am using Delphi 2009 and I’m using Unicode strings in my program.
So the final answer here is to override the Ansi compares with whatever you want (e.g. non-ansi compares) as follows:
type
TMyStringList = class(TStringList)
protected
function CompareStrings(const S1, S2: string): Integer; override;
end;
function TMyStringList.CompareStrings(const S1, S2: string): Integer;
begin
if CaseSensitive then
Result := CompareStr(S1, S2)
else
Result := CompareText(S1, S2);
end;