У меня есть 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;
Основная проблема заключается в том, что список заполняется в порядке с деталями, которые я хочу, но он никогда не сортируется.
Я вижу, что мой вопрос может привести к предположению, что я загружаю элементы из файла - это не так. Я делаю поиск двух каталогов для файлов определенного типа и их добавления в список. После этого я хочу, чтобы список сортировался. – OZ8HP
Все равно код в вопросе приведет к сортировке списка.Поэтому я думаю, вы должны сделать короткий SSCCE, чтобы показать, что вы имеете в виду. –
Я не уверен на 100%, что такое SSCCE :-) Я отредактировал вопрос, чтобы добавить еще код. – OZ8HP