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

# TFileItemCompareEvent

Implements a handler routine used to compare values in a custom file sort for TCustomShellTreeView.

## Declaration

Source position: shellctrls.pas line 73

`type TFileItemCompareEvent = function(`
`Item1: TFileItem;`
`Item2: TFileItem`
`):Integer of object;`

## Arguments

`Item1` |  | First file item for the comparison routine.
`Item2` |  | Second file item for the comparison routine.

## Function result

Integer with the relative sort order for the compared file items.

## Description

TFileItemCompareEvent is the type used to implement the OnSortCompare event handler in TCustomShellTreeView. TFileItemCompareEvent compares the file items specified in the Item1 and Item2 arguments to determine which value occurs first in the sort order.

The return value is an Integer with the relative sort order for the compared values:

A negative value (<0)
  Indicates that Item1 occurs before Item2 in the sort order.
A positive value (>0)
  Indicates that Item1 occurs after Item2 in the sort order.
0 (zero)
  Indicates that Item1 and Item2 have the same value in the sort order.

An application can implement and assign a routine using the signature to the OnSortCompare event handler in TCustomShellTreeView. Set its FileSortType property to fstCustom to enable the handler routine.

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;

## See also

TCustomShellTreeView.OnSortCompare |  | Event handler signalled to compare file items in a custom sort routine.
TCustomShellTreeView.FileSortType |  | Indicates the sort type used for items (tree nodes) on the tree view control.
TFileItem |  | Provides information about a file or directory on the local file system for use in file sort comparison routines.