Raz-Soft » Blog Archive » Sorting a ListView Buy Cheap Software
“Manual” YM Buddy remover »
« Drag & Drop Files

Sorting a ListView

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:

C++:
  1. int ColumnToSort; // add this global var in the private section of your TForm1 Class....
  2.  
  3. //---------------------------------------------------------------------------
  4.  
  5. void __fastcall TForm1::ListView1ColumnClick(TObject *Sender,
  6.       TListColumn *Column)
  7. {
  8.  
  9.    ColumnToSort = Column->Index;
  10.    ListView1->AlphaSort()//trigger ListView1Compare
  11.  
  12.   if (ColumnToSort==0)
  13.     ListView1->Tag=(ListView1->Tag==1) ? 0:1 ;
  14.   else
  15.     ListView1->Columns->Items[ColumnToSort-1]->Tag=(ListView1->Columns->Items[ColumnToSort-1]->Tag==1) ? 0:1;
  16.  
  17. }
  18. //---------------------------------------------------------------------------
  19.  
  20. void __fastcall TForm1::ListView1Compare(TObject *Sender, TListItem *Item1,
  21.       TListItem *Item2, int Data, int &Compare)
  22. {
  23.    AnsiString Str1,Str2;
  24.    int ColTag;
  25.  
  26.   if (ColumnToSort == 0)
  27.    {
  28.     ColTag=ListView1->Tag;
  29.     Str1=Item1->Caption;
  30.     Str2=Item2->Caption;
  31.    }
  32.   else
  33.    {
  34.     ColTag=ListView1->Columns->Items[ColumnToSort-1]->Tag;
  35.     Str1=Item1->SubItems->Strings[ColumnToSort-1];
  36.     Str2=Item2->SubItems->Strings[ColumnToSort-1];
  37.    }
  38.  
  39.   if (ColTag==1)
  40.      Compare=CompareText(Str1,Str2);
  41.   else
  42.      Compare=CompareText(Str2,Str1);
  43.  
  44. }
  45. //---------------------------------------------------------------------------

Add some items to your list and test it. Clicking on the same column will switch between ascending and descending sort:

C++:
  1. void __fastcall TForm1::FormCreate(TObject *Sender)
  2. {
  3.     ColumnToSort=0;
  4.         //add some items to test ListView sorting...
  5.     TListItem *tli=ListView1->Items->Add();
  6.     tli->Caption="A item ";
  7.     tli->SubItems->Add("B subitem1");
  8.     tli->SubItems->Add("C subitem2");
  9.  
  10.     tli=ListView1->Items->Add();
  11.     tli->Caption="B item ";
  12.     tli->SubItems->Add("E subitem1");
  13.     tli->SubItems->Add("A subitem2");
  14.  
  15.     tli=ListView1->Items->Add();
  16.     tli->Caption="C item ";
  17.     tli->SubItems->Add("A subitem1");
  18.     tli->SubItems->Add("E subitem2");
  19.  
  20.     tli=ListView1->Items->Add();
  21.     tli->Caption="D item ";
  22.     tli->SubItems->Add("C subitem1");
  23.     tli->SubItems->Add("D subitem2");
  24.  
  25.     tli=ListView1->Items->Add();
  26.     tli->Caption="E item ";
  27.     tli->SubItems->Add("D subitem1");
  28.     tli->SubItems->Add("B subitem2");
  29. }


related things:

Author: Raz | On February 17th, 2007 | C++ Builder, [ En ] | 3 Comments

Q: So what can I do next?
A: You can Buy me a Beer or Coffee. You can say Hi! or peek on the related stuff. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

    3 Responses to “Sorting a ListView”

  1. Anonymous NEW ZEALAND Says:

    Great stuff, thanks! exactly the code snippit I was looking for :)


  2. Raz ROMANIA Says:

    You're welcome, I'm glad to hear that this snippet helped you :)


  3. Joiner A. VENEZUELA Says:

    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


Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong> and all YM emotions