[Overview][Types][Classes][Procedures and functions][Index] | Reference for unit 'ShellCtrls' (#lcl)

# TCustomShellTreeView.OnSortCompare

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

## Declaration

Source position: shellctrls.pas line 144

`public property TCustomShellTreeView.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;

## Version info

Added in LCL version 3.0.

## See also

TCustomShellTreeView.FileSortType |  | Indicates the sort type used for items (tree nodes) on the tree view control.
TCustomShellTreeView.Items |  | Contains the tree nodes used to represent the hierarchical tree structure for the tree view control.
TCustomShellTreeView.ObjectTypes |  | Indicates the file system objects displayed using the control.
TCustomShellTreeView.Root |  | Indicates the directory or path which is the top-level node in the tree view.
TCustomShellTreeView.Path |  | Path to the directory displayed in the shell control.
TCustomShellTreeView.ExistsAndIsValid |  | Checks whether the specified path is a valid file system object and type for the tree view control.
TCustomShellTreeView.PopulateTreeNodeWithFiles |  | Adds tree nodes for file system objects found starting at the specified node / path.
TCustomShellTreeView.PopulateWithBaseFiles |  | Fills the tree view when the Root directory is empty.
TFileSortType |  | Represents sorting options for the items in a shell control.