Skip to content

Commit 9a7fe26

Browse files
refactor: changed the TComparer to use UTF8 instead of default ANSI
1 parent bd381d9 commit 9a7fe26

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

entries/dcornelius/src/uChallengeWithDictionary.pas

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ implementation
2222
type
2323
TWeatherCityList = TDictionary<string, TWeatherCity>;
2424

25+
TUTF8CustomComparer = class(TInterfacedObject, IComparer<string>)
26+
public
27+
function Compare(const Left, Right: string): Integer;
28+
end;
29+
2530
var
2631
WeatherCityList: TWeatherCityList;
2732

@@ -43,7 +48,7 @@ procedure ChallengeWithDictionary;
4348

4449
// create the output list
4550
CityArray := WeatherCityList.Keys.ToArray;
46-
TArray.Sort<string>(CityArray, TStringComparer.Ordinal);
51+
TArray.Sort<string>(CityArray, TUTF8CustomComparer.Create);
4752
FirstOutput := True;
4853
Write('{');
4954
for var City in CityArray do
@@ -60,4 +65,12 @@ procedure ChallengeWithDictionary;
6065
end;
6166
end;
6267

68+
{ TUTF8CustomComparer }
69+
70+
function TUTF8CustomComparer.Compare(const Left, Right: string): Integer;
71+
begin
72+
// Convert UTF-8 strings to UnicodeString for comparison
73+
Result := CompareStr(UTF8ToWideString(Left), UTF8ToWideString(Right));
74+
end;
75+
6376
end.

0 commit comments

Comments
 (0)