I think TStringList is eminently suitable for this task.
Here's an alternative solution, which may use less resources.
unit mainSortCount;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, StdCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
procedure FormCreate(Sender: TObject);
end;
var
Form1: TForm1;
procedure SortCount(const anInFile: String; out aList: TStringList);
procedure ShowListInMemo(constref aList: TStringList; aMemo: TMemo);
implementation
{$R *.lfm}
{ TForm1 }
procedure SortCount(const anInFile: String; out aList: TStringList);
const
one = PtrUInt(1);
var
textf: TextFile;
s: String;
idx: Integer;
function GetSuccObj(anIntObj: TObject): TObject;
var
i: PtrUInt absolute anIntObj;
begin
Inc(i);
Exit(anIntObj);
end;
begin
Assert(FileExists(anInFile), 'cannot find file "'+anInFile+'"');
aList := TStringList.Create;
aList.Duplicates := dupError;
aList.Sorted := True;
AssignFile(textf, anInFile);
try
Reset(textf);
while not EOF(textf) do
begin
ReadLn(textf, s);
s := Trim(s);
idx := aList.IndexOf(s);
case idx of
-1: aList.AddObject(s, TObject(one));
else
aList.Objects[idx] := GetSuccObj(aList.Objects[idx]);
end;
end;
finally
CloseFile(textf);
end;
end;
procedure ShowListInMemo(constref aList: TStringList; aMemo: TMemo);
var
i: Integer;
begin
if Assigned(aList) and Assigned(aMemo) then
begin
aMemo.Clear;
for i := 0 to aList.Count-1 do
aMemo.Lines.Add('%s - %d', [aList[i], PtrUInt(aList.Objects[i])]);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
sl: TStringList;
begin
SortCount('infile.txt', sl);
try
ShowListInMemo(sl, Memo1);
Memo1.Lines.SaveToFile('outfile.txt');
finally
sl.Free;
end;
end;
end.
howardpc question to you
I can't develop your code for the next need.
I want to count duplicates (or multi x3,x4...) from a CSV file according to DATETIME.
As a result (memo1) I want: DateTIME; Count; all Device_ID participating in the duplicate
Format of CSV file :
user_id,device_id,"datetime"
18817,13174,2020-01-09 00:01:14
15190,10604,2020-01-09 00:09:04
15190,10604,2020-01-09 00:09:05
10892,7559,2020-01-04 10:02:21
10892,7559,2020-01-04 10:52:59
10892,7559,2020-01-04 13:56:42
10892,7559,2020-01-04 20:46:01
15190,10604,2020-01-09 00:13:48
15190,10604,2020-01-09 00:13:48
6521,4879,2020-01-09 00:14:53