unit Unit1;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Controls.Presentation, FMX.StdCtrls, FMX.Layouts,
FMX.TreeView;
type
TForm1 =
class(TForm)
TreeView1: TTreeView;
BtFillTreeView: TButton;
BtSortDesc: TButton;
BtSortAsc: TButton;
procedure BtFillTreeViewClick(Sender: TObject);
procedure BtSortDescClick(Sender: TObject);
procedure BtSortAscClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
FlagSortDescending: Boolean;
function TreeView1Compare(Item1, Item2: TTreeViewItem): Integer;
function CompareItems(Item1, Item2: TFmxObject): Integer;
procedure SortItems;
public
end;
var
Form1: TForm1;
implementation
{$R *.fmx}
procedure TForm1.BtFillTreeViewClick(Sender: TObject);
var
TreeViewItem, TreeViewSubItem: TTreeViewItem;
i, j: Integer;
begin
for i := 0
to 9
do
begin
TreeViewItem := TTreeViewItem.Create(TreeView1);
TreeViewItem.Parent := TreeView1;
TreeViewItem.Text := i.ToString;
TreeViewItem.Tag := i;
for j := 0
to 9
do
begin
TreeViewSubItem := TTreeViewItem.Create(TreeView1);
TreeViewSubItem.Parent := TreeViewItem;
TreeViewSubItem.Text := j.ToString;
TreeViewSubItem.Tag := j;
end;
end;
end;
procedure TForm1.BtSortDescClick(Sender: TObject);
begin
FlagSortDescending := TRUE;
SortItems;
// das sortiert nur die "Ebene 0"...
SortItems;
// das sortiert dann auch die SubItems...
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
TreeView1.OnCompare := TreeView1Compare;
end;
procedure TForm1.BtSortAscClick(Sender: TObject);
begin
FlagSortDescending := FALSE;
SortItems;
// das sortiert nur die "Ebene 0"...
SortItems;
// das sortiert dann auch die SubItems...
end;
procedure TForm1.SortItems;
var
i: Integer;
TreeViewItem: TTreeViewItem;
begin
TreeView1.Sorted := FALSE;
TreeView1.Sorted := TRUE;
for i := 0
to TreeView1.Count-1
do
begin
TreeViewItem := TreeView1.Items[i];
TreeViewItem.Sort(CompareItems);
end;
TreeView1.Repaint;
end;
function TForm1.TreeView1Compare(Item1, Item2: TTreeViewItem): Integer;
begin
Result := Item1.Tag - Item2.Tag;
if FlagSortDescending
then
Result := -Result;
end;
function TForm1.CompareItems(Item1, Item2: TFmxObject): Integer;
begin
Result := Item1.Tag - Item2.Tag;
if FlagSortDescending
then
Result := -Result;
end;
end.