Solved

Sorting with CListView and LVN_GETDISPINFO

Posted on 2004-09-22
5
688 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

773 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