Skip to content

Commit f07e109

Browse files
refactor: Change output sort from TStringList to using TStringComparer
1 parent 2925285 commit f07e109

3 files changed

Lines changed: 24 additions & 21 deletions

File tree

entries/dcornelius/src/uChallengeCommon.pas

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ TWeatherCity = class
2020
constructor Create(const NewCityName: string; const NewTemp: Integer);
2121
procedure AddNewTemp(const NewTemp: Integer);
2222
function Mean: Double;
23-
function OutputSumLine: string;
23+
function OutputSumLine(const FirstOutput: Boolean): string;
2424
end;
2525

2626
// anonymous method type
@@ -169,13 +169,17 @@ function TWeatherCity.Mean: Double;
169169
Result := ChallengeCommon.PascalRound(TotalTemp / DataCount);
170170
end;
171171

172-
function TWeatherCity.OutputSumLine: string;
172+
function TWeatherCity.OutputSumLine(const FirstOutput: Boolean): string;
173173
begin
174-
Result := Format(' %s=%0.1f/%0.1f/%0.1f', [CityName,
175-
MinTemp / 10,
176-
Mean,
177-
MaxTemp / 10]);
174+
if FirstOutput then
175+
Result := ''
176+
else
177+
Result := ', ';
178178

179+
Result := Result + Format('%s=%0.1f/%0.1f/%0.1f', [CityName,
180+
MinTemp / 10,
181+
Mean,
182+
MaxTemp / 10]);
179183
end;
180184

181185
end.

entries/dcornelius/src/uChallengeWithDictionary.pas

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ procedure ChallengeWithDictionary;
1616
implementation
1717

1818
uses
19-
System.Classes, System.SysUtils, System.Generics.Collections, System.StrUtils,
19+
System.Classes, System.SysUtils, System.Generics.Collections, System.StrUtils, System.Generics.Defaults,
2020
uChallengeCommon;
2121

2222
type
@@ -27,8 +27,9 @@ implementation
2727

2828
procedure ChallengeWithDictionary;
2929
var
30-
SortedList: TStringList;
30+
CityArray: TArray<string>;
3131
CurrWeatherCity: TWeatherCity;
32+
FirstOutput: Boolean;
3233
begin
3334
WeatherCityList := TDictionary<string, TWeatherCity>.Create;
3435
try
@@ -41,18 +42,16 @@ procedure ChallengeWithDictionary;
4142
end);
4243

4344
// create the output list
44-
SortedList := TStringList.Create(TDuplicates.dupAccept, False, True);
45-
try
46-
for var WeatherSum in WeatherCityList do
47-
SortedList.Append(WeatherSum.Value.OutputSumLine);
48-
49-
// sort and write out the data
50-
SortedList.Sort;
51-
SortedList.QuoteChar := #0;
52-
Writeln('{', Trim(SortedList.DelimitedText), '}');
53-
finally
54-
SortedList.Free;
45+
CityArray := WeatherCityList.Keys.ToArray;
46+
TArray.Sort<string>(CityArray, TStringComparer.Ordinal);
47+
FirstOutput := True;
48+
Write('{');
49+
for var City in CityArray do
50+
begin
51+
Write(WeatherCityList.Items[City].OutputSumLine(FirstOutput));
52+
FirstOutput := False;
5553
end;
54+
Writeln('}');
5655
{$IFDEF DEBUG}
5756
Writeln('Unique Stations: ', WeatherCityList.Count);
5857
{$ENDIF}

entries/dcornelius/src/uChallengeWithStringList.pas

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ procedure ChallengeWithStringList;
7474
SortedList.Sort;
7575

7676
Write('{');
77-
Write(Trim(TWeatherCity(SortedList.Objects[0]).OutputSumLine));
77+
Write(Trim(TWeatherCity(SortedList.Objects[0]).OutputSumLine(True)));
7878
for var i := 1 to SortedList.Count - 1 do
79-
Write(TWeatherCity(SortedList.Objects[i]).OutputSumLine);
79+
Write(TWeatherCity(SortedList.Objects[i]).OutputSumLine(False));
8080
Writeln('}');
8181
{$IFDEF DEBUG}
8282
Writeln('Unique Stations: ', SortedList.Count);

0 commit comments

Comments
 (0)