Solved

Sorting with CListView and LVN_GETDISPINFO

Posted on 2004-09-22
5
684 Views
Last Modified: 2013-11-20
Hi,

I have experience with MFC and list views, but this is the first time I have to tackle sorting output and I'm having difficulties understanding how to do it.

I have 2 views, one of which (the easier one) I got sorting to work OK with - and now I would like to use similar code to get the other one to work. So the first list is basically a list of computer items, and I added the following code (the rest of the class is omitted for simplicity):

static BOOL bSortAscending;
static int iColumn;

void CRemoteUpdateView::OnLvnColumnclick(NMHDR *pNMHDR, LRESULT *pResult)
{
      LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
      
       NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
       iColumn = pNMListView->iSubItem;                        // Column starting with 0

      for (int i = 0; i < listCtrl.GetItemCount(); i++)
            listCtrl.SetItemData(i, i);

      listCtrl.SortItems(CompareItems, (LPARAM) &listCtrl);

      if (bSortAscending)
            bSortAscending = FALSE;
      else
            bSortAscending = TRUE;

      *pResult = 0;
}

static int CALLBACK CompareItems(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
      int iReturn = 0;

// lParamSort contains a pointer to the list view control.
      CListCtrl* pListCtrl = (CListCtrl*) lParamSort;

      CString strItem1 = pListCtrl->GetItemText(lParam1, iColumn);
      CString strItem2 = pListCtrl->GetItemText(lParam2, iColumn);

      if (bSortAscending)
            iReturn = strcmp(strItem2, strItem1);
      else
            iReturn = (strcmp(strItem2, strItem1) * -1);

      return iReturn;
}

Now, I have to admit that I don't 100% understand it, especially the

      for (int i = 0; i < listCtrl.GetItemCount(); i++)
            listCtrl.SetItemData(i, i);

part. Why do I need to assign numeric values to those items?

Anyhow, the 2nd view is different as I am using LVN_GETDISPINFO rather than inserting items with InsertItem() for speed reasons. This view can sometimes have 100.000 items in it.

So I have a memory structure that holds the information pulled by the OnGetdispinfo() function.

Now I can't use the code I used for the other view, since I can't call SetItemData() anymore and I'm a bit stuck.

If somebody can point out how to do this and also explain how the sorting feature works? It is my understanding the the lParam member of the item is what tells the SortItems() function the sequence, but what if I use LVN_GETDISPINFO?

Thank you.
0
Comment
Question by:Netikus
  • 2
  • 2
5 Comments
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 300 total points
ID: 12130545
The first question - the SetItemData.  That is just putting a unique value to each item in the list.

Your second case - called a virtual list by the way - you will have to sort the contents of your internal structure yourself in response to the header click event.
IF it comes from a database the quickest sort may be to rerun the query with the new sort and refill your internal structure again.
0
 
LVL 1

Author Comment

by:Netikus
ID: 12134295
OK. I understand that I am just putting a unique item in the list. Is it necessary to specify sequential numbers - I assume it is - right? Why doesn't it automatically go through the list one-by-one from the top to bottom - this would seem logical to me rather than calling SetItemData. What happens if one is already using the lParam parameter of the item structures (which I am doing in other cases) for other purposes - wouldn't those be overwritten?

The virtual list makes sense of course. I will work on that asap.

In the virtual list the data is actually stored in data structures, so sorting that will be a bit more difficult since it's not coming from a database. I'll do some research on sorting algorithms since I haven't dealt with sorting in a long time.


Thanks!
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 12136171
First point re numbers/SetItemData - I haven't studied that piece of code so I don't know why they are assigning a sequential number.  If you are already using that for storage of other info then ignore the assignment lines.  

The compare items should just return a value to say if the items should be swapped.  (It is a very simple but inefficient sorting mechanism used there by default.)
0
 
LVL 6

Assisted Solution

by:Amritpal Singh
Amritpal Singh earned 200 total points
ID: 12141522
0
 
LVL 1

Author Comment

by:Netikus
ID: 12154990
Thanks a lot guys, both the answers helped me finally understand and solve this.

For the large virtual list I had to refer to an algorithm theory book and use Quicksort, which works very nicely and fast - even for 20.000 items and more.

I also understand now that it is through the values of lParam that the compare function knows which items to compare. As such, the lParam values are integral for sorting. Without them set properly, the compare algorithm does not know which items to compare.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now