2014-03-06 34 views
1

我有一个的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; 

的主要问题是,名单充满OK与我想要的物品,但它永远不会被排序。

回答

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; 

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


但即使这并不能解释你的报告。因为如果拨打LoadFromFileSortedTrue,则会按顺序插入每条新行。所以,你在这个问题上报告的内容不可能是整个故事。


除非你正在后续添加到列表中,它是清洁的,在我看来,忽略Sorted财产。将Sorted作为其默认值False。如果您想对列表执行排序,请致电Sort。尽管如此,还是值得深入了解一下,以便理解为什么你的问题中的断言与实施TStringList不一致。

+0

我可以看到,我的问题可能导致假设我从文件加载项目 - 情况并非如此。我所做的是在2个目录中搜索某种类型的文件并将它们添加到列表中。之后,我希望列表进行排序。 – OZ8HP

+0

完全相同,问题中的代码将导致列表被排序。所以我认为你应该做一个简短的SSCCE来展示你的意思。 –

+0

我不是100%确定SSCCE是什么:-)我编辑了这个问题以包含更多的代码。 – OZ8HP