Help, please, sort the list in a ListView on the basis of first folders and then files. Here is the code:

procedure TForm1.FormCreate(Sender: TObject); var WFD:WIN32_Find_Data; HFile:THandle; Attr,Path:String; begin ListView1.Items.BeginUpdate;{Начинаем прорисовку} ListView1.Items.Clear; Path:=IncludeTrailingPathDelimiter('C:\Windows'); //Добавляем '\' при его отсутствиии HFile:=FindFirstFile(PChar(Path+'*.*'),WFD); //Ищем файлы только в указанном каталоге if HFile<>INVALID_HANDLE_VALUE then //Проверяем отсутствие ошиьок begin repeat if (WFD.dwFileAttributes and faHidden)<>2 then //Условие показыать\не показывать скрытые файлы if StrPas(WFD.cFileName)<>'.' then with ListView1.Items.Add do begin if (WFD.dwFileAttributes and faDirectory)<>faDirectory then begin Caption:=WFD.cFileName; SubItems.Add(ExtractFileExt(WFD.cFileName)); //Выделяем расширение SubItems.Add('размер'); //Размер файла end else begin Caption:=(WFD.cFileName); //Имя папки SubItems.Add('[Папка]'); //Указываем что это папка SubItems.Add('размер'); end; SubItems.Add('Дата'); //Время создания папки\файла Attr:='----'; if (WFD.dwFileAttributes and faArchive)<>0 then Attr[1]:='a'; //Архивный if (WFD.dwFileAttributes and faReadOnly)<>0 then Attr[2]:='r' //Только чтение if (WFD.dwFileAttributes and faHidden)<>0 then Attr[3]:='h'; //Скрытый if (WFD.dwFileAttributes and faSysFile)<>0 then Attr[4]:='s'; //Системный SubItems.Add(Attr); //Атрибуты файла\папки end; application.ProcessMessages; // Чтобы форма не сильно тормозила во время поиска until FindNextFile(HFile,WFD)<>True; Winapi.Windows.FindClose(HFile); end; ListView1.Items.EndUpdate; //Заканчиваем прорисовку файлов end; 

    1 answer 1

    You need to make your structure, fill it with data, using FindFirstFile / FindNexFile . Then sort as needed, and only then display in the ListView .

     type TMyFileInfo = record Name: string; Extension: string; Size: UInt64; Attributes: string; // добавьте что вам там еще надо end; var MyFiles: array of TMyFileInfo; 

    Fill the MyFiles array, then sort, then output.

    You can write an event handler for the OnCompare event for the ListView . There compare records ListView as you wish. How to compare, and sorted. Read the help how to work with it.

    Ideally, you should make the ListView virtual, make your list of entries and sort them there.