Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Delphi 为什么我的TStringList没有分类_Delphi_Sorting_Tstringlist - Fatal编程技术网

Delphi 为什么我的TStringList没有分类

Delphi 为什么我的TStringList没有分类,delphi,sorting,tstringlist,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.ScriptsL

我在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;
但是列表没有排序 为什么呢

编辑: 通过以下代码填写列表

  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;
主要的问题是,列表中填充了我想要的项目,但它从未被排序。

如果将“排序”设置为True,则表示希望列表按顺序维护。添加新项目时,它们将按顺序插入。当Sorted为True时,Sort方法不执行任何操作,因为代码是基于列表已经排序的假设构建的

因此,在代码调用中,Sort不起任何作用,可以被删除。但是,我将采用另一种方法,删除Sorted的设置并显式调用Sort:

ScriptList.LoadFromFile(...);
ScriptList.Sort;
for i := 0 to ScriptList.Count - 1 do
  ...
事实上,我认为你的代码并不像你所说的那样。您声明加载了该文件,然后将“排序”设置为True。事实并非如此。以下是SetSorted实现:

procedure TStringList.SetSorted(Value: Boolean);
begin
  if FSorted <> Value then
  begin
    if Value then Sort;
    FSorted := Value;
  end;
end;
因此,如果将“排序”设置为True时为False,则列表将被排序

但即使这样也不能解释你的报道。因为当调用LoadFromFile时,如果Sorted为True,则将按顺序插入每一新行。因此,你在问题中报告的内容不可能是全部

在我看来,除非您要对列表进行后续添加,否则忽略Sorted属性会更干净。将排序保留为其默认值False。当您想要对列表强制排序时,调用Sort。尽管如此,还是值得深入挖掘一下,以理解为什么您在问题中的断言与TStringList的实现不符。

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

因此,在代码调用中,Sort不起任何作用,可以被删除。但是,我将采用另一种方法,删除Sorted的设置并显式调用Sort:

ScriptList.LoadFromFile(...);
ScriptList.Sort;
for i := 0 to ScriptList.Count - 1 do
  ...
事实上,我认为你的代码并不像你所说的那样。您声明加载了该文件,然后将“排序”设置为True。事实并非如此。以下是SetSorted实现:

procedure TStringList.SetSorted(Value: Boolean);
begin
  if FSorted <> Value then
  begin
    if Value then Sort;
    FSorted := Value;
  end;
end;
因此,如果将“排序”设置为True时为False,则列表将被排序

但即使这样也不能解释你的报道。因为当调用LoadFromFile时,如果Sorted为True,则将按顺序插入每一新行。因此,你在问题中报告的内容不可能是全部


在我看来,除非您要对列表进行后续添加,否则忽略Sorted属性会更干净。将排序保留为其默认值False。当您想要对列表强制排序时,调用Sort。尽管如此,为了理解您在问题中的断言与TStringList的实现不符的原因,可能需要深入挖掘一点。

我可以看出,我的问题可能会导致我从文件加载项的假设,但事实并非如此。我要做的是在2个目录中搜索特定类型的文件,并将它们添加到列表中。之后,我希望列表进行排序。同样,问题中的代码将导致列表被排序。所以我认为你应该做一个简短的SSCCE来说明你的意思。我不是100%确定SSCCE是什么:-我已经编辑了这个问题以包含更多的代码。你可以使用Google来查找SSCCE isI可以看到的内容,我的问题可能会导致我从文件加载项目的假设-事实并非如此。我要做的是在2个目录中搜索特定类型的文件,并将它们添加到列表中。之后,我希望列表进行排序。同样,问题中的代码将导致列表被排序。因此,我认为你应该做一个简短的SSCCE来说明你的意思。我不是100%确定SSCCE是什么:-我已经编辑了这个问题以包含更多的代码。你可以使用谷歌来找到SSCCE是什么