Solved

Sorting with CListView and LVN_GETDISPINFO

Posted on 2004-09-22
5
711 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

623 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