Here is how you can use the OnColumnClick and OnCompare events of a TListView to let users sort the columns (ascending/descending) in a report-style list view by clicking on the column headers. This requires a global variable to keep track of the column that was clicked:
[cpp]
int ColumnToSort; // add this global var in the private section of your TForm1 Class….
//—————————————————————————
void __fastcall TForm1::ListView1ColumnClick(TObject *Sender,
TListColumn *Column)
{
ColumnToSort = Column->Index;
ListView1->AlphaSort(); //trigger ListView1Compare
if (ColumnToSort==0)
ListView1->Tag=(ListView1->Tag==1) ? 0:1 ;
else
ListView1->Columns->Items[ColumnToSort-1]->Tag=(ListView1->Columns->Items[ColumnToSort-1]->Tag==1) ? 0:1;
}
//—————————————————————————
void __fastcall TForm1::ListView1Compare(TObject *Sender, TListItem *Item1,
TListItem *Item2, int Data, int &Compare)
{
AnsiString Str1,Str2;
int ColTag;
if (ColumnToSort == 0)
{
ColTag=ListView1->Tag;
Str1=Item1->Caption;
Str2=Item2->Caption;
}
else
{
ColTag=ListView1->Columns->Items[ColumnToSort-1]->Tag;
Str1=Item1->SubItems->Strings[ColumnToSort-1];
Str2=Item2->SubItems->Strings[ColumnToSort-1];
}
if (ColTag==1)
Compare=CompareText(Str1,Str2);
else
Compare=CompareText(Str2,Str1);
}
//—————————————————————————
[/cpp]
Add some items to your list and test it. Clicking on the same column will switch between ascending and descending sort:
[cpp]
void __fastcall TForm1::FormCreate(TObject *Sender)
{
ColumnToSort=0;
//add some items to test ListView sorting…
TListItem *tli=ListView1->Items->Add();
tli->Caption=”A item “;
tli->SubItems->Add(“B subitem1″);
tli->SubItems->Add(“C subitem2″);
tli=ListView1->Items->Add();
tli->Caption=”B item “;
tli->SubItems->Add(“E subitem1″);
tli->SubItems->Add(“A subitem2″);
tli=ListView1->Items->Add();
tli->Caption=”C item “;
tli->SubItems->Add(“A subitem1″);
tli->SubItems->Add(“E subitem2″);
tli=ListView1->Items->Add();
tli->Caption=”D item “;
tli->SubItems->Add(“C subitem1″);
tli->SubItems->Add(“D subitem2″);
tli=ListView1->Items->Add();
tli->Caption=”E item “;
tli->SubItems->Add(“D subitem1″);
tli->SubItems->Add(“B subitem2″);
}
[/cpp]

Programmers should be trusted. If your brain surgeon told you the operation you need takes five hours, would you pressure him to do it in three? 
Thank you, Raz! It’s great, and it’s helps me in my busyness.
Hi dude,
Your code snippet is perfect for my use and works very fine.
Thanks very much
oh my god!!! thxxx dude. im looking for this long time ago… its exactly what i need.
pd: in the code posted here the sorting its made descending/ascending, just changing:
#
if (ColTag==1)
#
Compare=CompareText(Str1,Str2);
#
else
#
Compare=CompareText(Str2,Str1);
for this:
#
if (ColTag==1)
#
Compare=CompareText(Str2,Str1);
#
else
#
Compare=CompareText(Str1,Str2);
the sorting change to ascending/descending, at least in my proyect.
greetings from venezuela, sorry about my english xD
You’re welcome, I’m glad to hear that this snippet helped you
Great stuff, thanks! exactly the code snippit I was looking for