delphi - Why doesn't my TStringList gets sorted -
i have tstringlist create on formcreate
scriptlist := tstringlist.create;
in function in program after have loaded strings list have following code
scriptlist.sorted := true; scriptlist.sort; := 0 scriptlist.count - 1 showmessage(scriptlist[i]);
but list not sorted why that?
edited: filling list done following code
function tfrmmain.scriptslocate(const acomputer: boolean = false): integer; var scriptpath: string; templist: tstringlist; begin templist := tstringlist.create; try if acomputer = true begin scriptpath := folders.dirscripts; files.search(templist, scriptpath, '*.logon', false); scriptlist.addstrings(templist); end else begin if servercheck begin scriptpath := serverpath + 'scripts_' + network.computername + '\'; folders.validate(scriptpath); templist.clear; files.search(templist, scriptpath, '*.logon', false); scriptlist.addstrings(templist); application.processmessages; scriptpath := serverpath + 'scripts_' + 'shared\'; folders.validate(scriptpath); templist.clear; files.search(templist, scriptpath, '*.logon', false); scriptlist.addstrings(templist); end; end; templist.free; end; scriptlist.sort; result := scriptlist.count; end;
the filesearch function:
function tfiles.search(alist: tstringlist; apathname: string; const afile: string = '*.*'; const asubdirs: boolean = true): integer; var rec: tsearchrec; begin folders.validate(apathname, false); if findfirst(apathname + afile, faanyfile - fadirectory, rec) = 0 try repeat alist.add(apathname + rec.name); until findnext(rec) <> 0; findclose(rec); end; result := alist.count; if not asubdirs exit; if findfirst(apathname + '*.*', fadirectory, rec) = 0 try repeat if ((rec.attr , fadirectory) <> 0) , (rec.name<>'.') , (rec.name<>'..') files.search(alist, apathname + rec.name, afile, true); until findnext(rec) <> 0; findclose(rec); end; result := alist.count; end;
the main problem list filled ok items want, never gets sorted.
when set sorted
true
saying want list maintained in order. when new items added, inserted in order. when sorted
true
, sort
method nothing because code built on assumption list order.
so, in code calling sort
nothing , removed. however, take alternative approach, remove setting of sorted
, call sort
explicitly:
scriptlist.loadfromfile(...); scriptlist.sort; := 0 scriptlist.count - 1 ...
now, in fact think code not quite have claimed. claim load file, , set sorted
true
. cannot case. here setsorted
implementation:
procedure tstringlist.setsorted(value: boolean); begin if fsorted <> value begin if value sort; fsorted := value; end; end;
so, if sorted
false
when set true
, list sorted.
but not explain report. because if sorted
true
when call loadfromfile
, each new line inserted in order. so, report in question cannot whole story.
unless making subsequent additions list, cleaner, in view, ignore sorted
property. leave sorted
default value of false
. , call sort
when want enforce ordering list. same, might worth digging bit deeper understand why assertions in question don't tally implementation of tstringlist
.
Comments
Post a Comment