FAQ & Knowledge Base

Welcome to our Knowledge Base. Search or browse through the topics below to find answers to your questions.

Categories: ShellBrowser Delphi Components | Show all categories

Sorting custom columns

Question / Problem

I have added a custom column to the TJamShellList that adds date values for the individual items. The user can sort the column by clicking on the column header, but the column is not sorted by date, but alphabetically. How can I fix this?

Answer / Solution

ShellBrowser per default uses an alpha-numerical comparison method on the custom column texts of the items, that unfortunately doesn't respect date values.

You can however custom sort the affected columns by implementing the "OnCompare" event, e.g.:

procedure TMainForm.JamShellList1Compare(Sender: TObject; Item1,
  Item2: TListItem; Data: Integer; var Compare: Integer);
var lDate1, ldate2: TDateTime;
begin
  if JamShellList1.Columns[JamShellList1.SortColumn].Caption = 'Custom date column' then begin
    lDate1 := StrToDateTime(Item1.SubItems[JamShellList1.Columns[JamShellList1.SortColumn].SubItemIndex]);
    lDate2 := StrToDateTime(Item2.SubItems[JamShellList1.Columns[JamShellList1.SortColumn].SubItemIndex]);
    Compare := CompareDateTime(lDate1, lDate2);
  end;
end;