TStringList wrong behavior lazarus / 8-Other ---------------------------------------------------------------------- typo · 2010-05-16 18:46 Don't you think that putting accented words before all the other words is a wrong behavior? I think it is simply wrong... I don't know any language that do this kind of sorting. ---------------------------------------------------------------------- Martin_fr · 2010-05-16 21:34 I've seen that in databases, and afaik even windows has a setting to do so. There is no "correct" sorting order by default. As you mention it's language specific => it's called collation. TStringlist probably uses binary collation, in which letters are ordered by their numeric value; and not by any language specific rule. Using something like TListBox, may or may not leave the sorting to the Operating System, the Operating System can be set to a different collation. From http://en.wikipedia.org/wiki/Collation "A numeric sort may order ñ incorrectly following z " Actually it isn't incorrect. For a numeric (binary collation) this is the correct place. (it is only incorrect, if your intend was to sort by another collation, but then the error is that you used the wrong collation. And for some languages there is more than one collation available. http://de.wikipedia.org/wiki/Alphabetische_Sortierung gives 4 different ways how things can be sorted in German. TStringlist  has no setting to learn which collation you want to use (the language of the text doesn't matter, the collation does). So TStringlist sorts things correct, according to the collation it uses. ---------------------------------------------------------------------- typo · 2010-05-16 22:04 QuoteUsing something like TListBox, may or may not leave the sorting to the Operating System, the Operating System can be set to a different collation. But, if TListBox does the thing, why TStringList can't do? ---------------------------------------------------------------------- Martin_fr · 2010-05-16 22:17 As I said: TListbox is probably sorted by your Operating System. TStringList is OS independent, it doesn't call the OS. For many tasks the binary sort order is more than enough. eg keeping data sorted, so you can do binary search on it. --- Now that doesn't mean that it wouldn't be a nice feature. It only says, that the current behaviour isn't (imho) wrong as such. having said that, you can always use TstringList.CustomSort I don't know if there is any ready made collation sorting available, or if you have to write it from scratch.... ---------------------------------------------------------------------- theo · 2010-05-17 03:25 Didn't you ask this before? http://forum.lazarus.freepascal.org/index.php/topic,8757.0.html ---------------------------------------------------------------------- typo · 2010-05-17 04:09 Before I asked if this was intentional. Now I'm asking if this is right. ---------------------------------------------------------------------- typo · 2010-05-17 05:06 I think you'll answer it is the only one, so it is right ---------------------------------------------------------------------- Martin_fr · 2010-05-17 05:27 There even exists a bug: http://bugs.freepascal.org/view.php?id=15864 but the forum is a better place to discuss it. ---------------------------------------------------------------------- typo · 2010-05-17 05:34 AnsiCompare serves only to non-accented languages. ---------------------------------------------------------------------- typo · 2010-05-17 06:04 Perhaps something in Assembly. ---------------------------------------------------------------------- theo · 2010-05-17 07:23 Can you please show us which result you get and what you expect? Please try this code: Code: [Select]function UTF8Compare(List: TStringList; Index1, Index2: Integer): Integer; begin   Result:=WideCompareStr(UTF8Decode(List[Index1]),UTF8Decode(List[Inde x2])); end; procedure TForm1.Button1Click(Sender: TObject); var sl:TStringList; begin  sl:=TStringList.Create;  sl.Text:=Memo1.text;  sl.CustomSort(@UTF8Compare);  Memo2.Text:=sl.text;  sl.free; end; Does the result difffer from this chart (Windows Vista for PT_BR) ? http://www.collation- charts.org/vista/vista.0416.CP1252.Portuguese_Brazil.html ---------------------------------------------------------------------- typo · 2010-05-17 07:50 Well, thanks, it is very fast and I will use it, but why it is not in the TStringList.Sort method? Quotefunction TStringList.DefaultCompareText(const s1, s2: string): PtrInt; begin   if FCaseSensitive then     Result := AnsiCompareStr(s1, s2)   else     Result := AnsiCompareText(s1, s2); end; ---------------------------------------------------------------------- typo · 2010-05-17 07:57 QuoteProcedure TStringList.Sort; begin   CustomSort(@StringListAnsiCompare); end; ---------------------------------------------------------------------- theo · 2010-05-17 08:30 Because Unicode support in the RTL is not ready. See: http://wiki.freepascal.org/FPC_Unicode_support#Roadmap_of_RTL_Uni code_support_with_UTF8String ---------------------------------------------------------------------- typo · 2010-05-17 08:32 OK.