为什么我的TStringList没有排序

时间:2014-03-06 09:28:39

标签: delphi sorting tstringlist

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

  ScriptList := TStringList.Create;

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

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

但是列表没有排序 那是为什么?

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

完成
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;

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;

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

1 个答案:

答案 0 :(得分:8)

当您将Sorted设置为True时,您说您希望按顺序维护列表。添加新项目时,它们将按顺序插入。当SortedTrue时,Sort方法不执行任何操作,因为代码是基于列表已经订购的假设而构建的。

因此,在您的代码中调用Sort什么都不做,可以删除。不过,我会采用替代方法,删除Sorted设置并明确调用Sort

ScriptList.LoadFromFile(...);
ScriptList.Sort;
for i := 0 to ScriptList.Count - 1 do
  ...

现在,事实上我认为你的代码并不像你声称的那样。您声明已加载该文件,然后将Sorted设置为True。情况并非如此。以下是SetSorted实施:

procedure TStringList.SetSorted(Value: Boolean);
begin
  if FSorted <> Value then
  begin
    if Value then Sort;
    FSorted := Value;
  end;
end;

因此,如果将Sorted设置为False True,则会对列表进行排序。


但即使这样也无法解释你的报道。因为当您致电Sorted时,如果TrueLoadFromFile,则会按顺序插入每个新行。所以,你在问题中报告的内容不可能是整个故事。


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

相关问题