为什么我的TStringList没有排序

OZ8*_*8HP 1 delphi sorting tstringlist

我有一个我在FormCreate上创建的TStringList

  ScriptList := TStringList.Create;
Run Code Online (Sandbox Code Playgroud)

在将字符串加载到列表中后,在我的程序中的另一个函数中,我有以下代码

  ScriptList.Sorted := True;
  ScriptList.Sort;
  for i := 0 to ScriptList.Count - 1 do
    ShowMessage(ScriptList[i]);
Run Code Online (Sandbox Code Playgroud)

但列表没有排序为什么会这样?

已编辑:填写列表由以下代码完成

function TfrmMain.ScriptsLocate(const aComputer: boolean = False): integer;
var
  ScriptPath: string;
  TempList: TStringList;
begin
  TempList := TStringList.Create;
  try
    if aComputer = True then
      begin
        ScriptPath := Folders.DirScripts;
        Files.Search(TempList, ScriptPath, '*.logon', False);
        ScriptList.AddStrings(TempList);
      end
    else
      begin
        if ServerCheck then
          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;
  finally
    TempList.Free;
  end;
  ScriptList.Sort;
  Result := ScriptList.Count;
end;
Run Code Online (Sandbox Code Playgroud)

filesearch函数:

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 then
    try
      repeat
        aList.Add(aPathname + Rec.Name);
      until FindNext(Rec) <> 0;
    finally
      FindClose(Rec);
    end;
  Result := aList.Count;
  if not aSubdirs then Exit;
  if FindFirst(aPathname + '*.*', faDirectory, Rec) = 0 then
    try
      repeat
        if ((Rec.Attr and faDirectory) <> 0)  and (Rec.Name<>'.') and (Rec.Name<>'..') then
          Files.Search(aList, aPathname + Rec.Name, aFile, True);
        until FindNext(Rec) <> 0;
    finally
      FindClose(Rec);
    end;
  Result := aList.Count;
end;
Run Code Online (Sandbox Code Playgroud)

主要问题是列表已填充好我想要的项目,但它永远不会被排序.

Dav*_*nan 8

当您设置SortedTrue你说你想为了维持名单.添加新项目时,它们将按顺序插入.如果SortedTrue,该Sort方法不执行任何操作,因为代码是建立在该列表已经是顺序的假设.

因此,在您的代码调用Sort中什么都不做,可以删除.但是,我会采取替代方法,删除设置SortedSort明确调用:

ScriptList.LoadFromFile(...);
ScriptList.Sort;
for i := 0 to ScriptList.Count - 1 do
  ...
Run Code Online (Sandbox Code Playgroud)

现在,事实上我认为你的代码并不像你声称的那样.您声称加载该文件,然后设置SortedTrue.情况并非如此.这是SetSorted实施:

procedure TStringList.SetSorted(Value: Boolean);
begin
  if FSorted <> Value then
  begin
    if Value then Sort;
    FSorted := Value;
  end;
end;
Run Code Online (Sandbox Code Playgroud)

所以,如果SortedFalse当你将它设置为True,列表进行排序.


但即使这样也无法解释你的报道.因为如果SortedTrue当你打电话LoadFromFile,每一个新的行会,以便插入.所以,你在问题中报告的内容不可能是整个故事.


除非您在列表中进行后续添加,否则在我看来,忽略该Sorted属性更为清晰.保留Sorted为其默认值False.Sort当您想对列表强制执行排序时调用.尽管如此,可能值得进一步深入了解为什么你的问题中的断言不符合实现TStringList.