In the VCL System.Classes unit, the virtual function TStringList.CompareStrings
is called everywhere. Hence, when overriding CompareStrings
, custom sorting can be implemented. Unfortunately, although declared in "...\Core Source\System.Classes.pas", in the web core version CompareStrings
is never called and hence custom sorting fails.
EDIT:
And here is a workaround. Browsing through the TStringList implementation in "...\Core Source\System.Classes.pas", a protected Function DoCompareText(const s1,s2 : string) : PtrInt; virtual;
can be found. Using this instead of the "regular" CompareStrings
does the trick. But as DoCompareText
is not exposed to Delphi, the usual magic has to be applied, like so:
Type
TLocaleStringList = Class(TStringList)
Private
FCollator : JSValue;
FLocale : String;
Protected
{$IfDef PAS2JS}
Function DoCompareText(const AnS1,AnS2 : string) : PtrInt; override;
{$EndIf}
Public
Constructor Create(Const ALocale : String = 'en-US');
Property Locale : String read FLocale;
End;
Constructor TLocaleStringList.Create(Const ALocale : String = 'en-US');
Begin
Inherited Create;
FLocale := ALocale;
CaseSensitive := False;
Sorted := True;
// Compiler honey...
If FCollator=Nil then;
// Create Intl.Collator
{$IfDef PAS2JS}
ASM
this.FCollator = new Intl.Collator(ALocale, {sensitivity: 'base'});
END;
{$EndIf}
End;
{$IfDef PAS2JS}
Function TLocaleStringList.DoCompareText(Const S1,S2 : String) : PtrInt;
Begin
// Compiler honey...
Result := 0;
// Compare
ASM
Result = this.FCollator.compare(S1, S2);
END;
End;
{$EndIf}