TreeView Sorting ?

Hi,
How to sort TreeView nodes by text or data?
please, support with source code example.

Regards,
Alim
alim_mazeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

FarajLYCommented:
try this
 TreeView1.SortType:=stText; Or  TreeView1.SortType:=stData;
0
pcsentinelCommented:
Sorry FarajLy I dont think thats right, the SortType determines when the data is sorted, not how it is sorted.

try this

******************UNIT CODE*********************
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    TreeView1: TTreeView;
    Button1: TButton;
    Button2: TButton;
    rbText: TRadioButton;
    rbData: TRadioButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure TreeView1Compare(Sender: TObject; Node1, Node2: TTreeNode;
      Data: Integer; var Compare: Integer);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
      TreeView1.Items.AddObject(nil,'Iiiiii',pointer(1));
      TreeView1.Items.AddObject(nil,'Eeeeee',pointer(5));
      TreeView1.Items.AddObject(nil,'Dddddd',pointer(6));
      TreeView1.Items.AddObject(nil,'Bbbbbb',pointer(8));
      TreeView1.Items.AddObject(nil,'Gggggg',pointer(3));
       TreeView1.Items.AddObject(nil,'Ffffff',pointer(4));
      TreeView1.Items.AddObject(nil,'Hhhhhh',pointer(2));
      TreeView1.Items.AddObject(nil,'Aaaaaa',pointer(9));
      TreeView1.Items.AddObject(nil,'Cccccc',pointer(7));
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
      TreeView1.Alphasort;
end;

procedure TForm1.TreeView1Compare(Sender: TObject; Node1, Node2: TTreeNode;
  Data: Integer; var Compare: Integer);
begin
      if rbText.Checked then
  begin
        if Node1.Text<Node2.Text then
          Compare:=-1
    else
          Compare:=1
  end
  else
        Compare:=integer(Node1.Data)-integer(Node2.data);
end;

end.


*************************FORM CODE*******************
object Form1: TForm1
  Left = 229
  Top = 173
  Width = 696
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object TreeView1: TTreeView
    Left = 20
    Top = 20
    Width = 245
    Height = 369
    Indent = 19
    SortType = stBoth
    TabOrder = 0
    OnCompare = TreeView1Compare
  end
  object Button1: TButton
    Left = 320
    Top = 36
    Width = 75
    Height = 25
    Caption = 'Load'
    TabOrder = 1
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 324
    Top = 120
    Width = 75
    Height = 25
    Caption = 'Sort'
    TabOrder = 2
    OnClick = Button2Click
  end
  object rbText: TRadioButton
    Left = 320
    Top = 68
    Width = 113
    Height = 17
    Caption = 'Text'
    Checked = True
    TabOrder = 3
    TabStop = True
  end
  object rbData: TRadioButton
    Left = 320
    Top = 88
    Width = 113
    Height = 17
    Caption = 'Data'
    TabOrder = 4
  end
end

**********************************************

regards
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
alim_mazeAuthor Commented:
thank you pcsentinel for your extended answer ...
all I needed actually was this line : TreeView1.Alphasort; or  TreeView1.Items.AlphaSort(true);

I tried it, and it's ok.

Regards,
Alim
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.