Sign in Join
FPDoc Onlineexperiment

TCustomShellTreeView.OnSortCompare property

Member of TCustomShellTreeViewpublicIn unit ShellCtrls

Event handler signalled to compare file items in a custom sort routine.

Declaration

pascal
property OnSortCompare: TFileItemCompareEvent read FOnSortCompare write SetOnSortCompare;

Description

OnSortCompare is a TFileItemCompareEvent property with the event handler signalled to implement a custom file sort for the tree view control. OnSortCompare is used to order the directories or files displayed in the tree view control when the FileSortType property is set to fstCustom.

Changing the routine assigned to the property causes the Items in the control to be reloaded and ordered at run-time starting at the top-level tree node in Root. The Path property is used to to expand and select a tree node after the sort operation has been completed if the path still exists and is valid for the settings in ObjectTypes.

An application can implement and assign a routine using the signature perform custom file comparison routines using various attributes. The following is an item compare function, as implemented by forum member d7_2_laz, used to order items with leading Underscore characters:

function TForm1.SortCompareUnderscore(Item1, Item2: TFileItem): integer;
begin
  // Make sure that folders are moved to the top
  Result := ord(Item2.isFolder) - ord(Item1.isFolder);
  if Result = 0 then
    if (pos('_', Item1.FileInfo.Name) = 1) or 
      (pos('_', Item2.FileInfo.Name) = 1) then
      Result := AnsiCompareText(Item1.FileInfo.Name, Item2.FileInfo.Name)
    else
      Result := CompareText(Item1.FileInfo.Name, Item2.FileInfo.Name);
end;

Sort File Items by Date

function TForm1.SortCompareByDate(Item1, Item2: TFileItem): integer;
begin
  // Folders first ...
  Result := ord(Item2.isFolder) - ord(Item1.isFolder);
  if Result = 0 then
  begin
    // then file date ...
    Result := CompareValue(Item1.FileInfo.TimeStamp, Item2.FileInfo.TimeStamp);
    if Result = 0 then
      // then file name
      Result := CompareText(Item1.FileInfo.Name, Item2.FileInfo.Name);
  end;
end;

Sort File Items by Size

function TForm1.SortCompareBySize(Item1, Item2: TFileItem): integer;
begin
  // Folders first
  Result := ord(Item2.isFolder) - ord(Item1.isFolder);
  if Result = 0 then
  begin
    // then file size ...
    Result := Item1.FileInfo.Size - Item2.FileInfo.Size;
    if Result = 0 then
      // then file name
      Result := CompareText(Item1.FileInfo.Name, Item2.FileInfo.Name);
  end;
end;

Added in LCL version 3.0.

See also

An experiment, built from development snapshots. Not the official Free Pascal or Lazarus documentation.