-4

I got this list of names in a textfile in a randomn order....how do I sort it in an alphabetical order (delphi) ? I need to keep it in the same text file. File looks like this:

Smith
Jack
Klein
Elliot

2 Answers2

0

I would use a TStringlist since it have a buildin function for sorting. Something like this:

var
  Stringlist : TStringlist;
begin
  Stringlist := TStringlist.Create;
  StringList.Loadfromfile(MYFILE);
  StringList.sort;
  StringList.saveToFile(MYFILLE);
  StringList.Free;
end;
Jens Borrisholt
  • 6,174
  • 1
  • 33
  • 67
-1

If every word occupies separate line, the simplest way is to use StringList class (I save result in another file for safer debug)

with TStringList.Create do try
  LoadFromfile('text.txt');
  Sort; 
  SaveToFile('sortedtext.txt');
finally
  Free;
end;
MBo
  • 77,366
  • 5
  • 53
  • 86