简体   繁体   English

为什么我的TStringList不排序

[英]Why doesn't my TStringList gets sorted

I Have a TStringList I create on FormCreate 我有一个在FormCreate上创建的TStringList

  ScriptList := TStringList.Create;

In another function in my program after I have loaded strings into the list I have the following code 在将字符串加载到列表中之后,在程序的另一个函数中,我具有以下代码

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

But the list is not sorted Why is that? 但是列表没有排序,为什么呢?

Edited: Filling the list is done by the following code 编辑:通过以下代码填充列表

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;

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

The main problem is that the list is filled OK with the items I want, but it never gets sorted. 主要问题是,列表中的内容已填满我想要的项目,但从未对其进行排序。

When you set Sorted to True you are saying that you want the list to be maintained in order. 当您将Sorted设置为True时,表示Sorted顺序维护列表。 When new items are added, they will be inserted in order. 添加新项目时,将按顺序插入它们。 When Sorted is True , the Sort method does nothing because the code is built on the assumption that the list is already order. SortedTrueSort方法不执行任何操作,因为代码是基于列表已经排序的假设构建的。

So, in your code calling Sort does nothing and could be removed. 因此,在您的代码中调用Sort不会执行任何操作,因此可以将其删除。 However, I would take the alternative approach, remove the setting of Sorted and call Sort explicitly: 但是,我将采用另一种方法,删除Sorted的设置并显式调用Sort

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

Now, in fact I think that your code is not quite as you have claimed. 现在,实际上我认为您的代码与您所声称的不太一样。 You claim that you load the file, and then set Sorted to True . 您声称已加载文件,然后将Sorted设置为True That cannot be the case. 事实并非如此。 Here is the SetSorted implementation: 这是SetSorted实现:

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

So, if Sorted is False when you set it to True , the list will be sorted. 因此,如果将Sorted设置为TrueSortedFalse ,则列表将被排序。


But even that does not explain what you report. 但是,即使那样也不能解释您的举报。 Because if Sorted is True when you call LoadFromFile , each new line will be inserted in order. 因为如果在调用LoadFromFile时如果SortedTrue ,那么将按顺序插入每一行。 So, what you report in the question cannot be the whole story. 因此,您在问题中所举报的内容可能并非全部。


Unless you are making subsequent additions to the list, it is cleaner, in my view, to ignore the Sorted property. 在我看来,除非您要对该列表进行后续添加,否则忽略Sorted属性会更干净。 Leave Sorted as its default value of False . Sorted保留为其默认值False And call Sort when you want to enforce an ordering to the list. 如果要对列表强制Sort ,请调用Sort All the same, it might be worth digging a bit deeper to understand why your assertions in the question don't tally with the implementation of TStringList . 尽管如此,可能有必要更深入地了解为什么问题中的断言与TStringList的实现TStringList

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM