Thread: Listview sorting...Can't make it work!

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    329

    Angry Listview sorting...Can't make it work!

    I've tried several approaches, but none of them seem to work...
    Here's my latest code for this.. Someone please help!

    From my windowproc's WM_NOTIFY:
    Code:
    		case LVN_COLUMNCLICK:			
    			// Keep track of sort order and column used for sorting.
    			static int nSortColumn = -1;
    			static BOOL bSortAscending = TRUE;
    			LVITEM lvi;
    
    			// Reverse sort order if column was just previously clicked.
    			if(((NM_LISTVIEW*)lParam)->iSubItem == nSortColumn)
    				bSortAscending = !bSortAscending;
    			else{
    				bSortAscending = TRUE; 
    				nSortColumn = ((NM_LISTVIEW*)lParam)->iSubItem;
    			}
    			// Set item data for each row to column value,
    			// since sort routine gets passed item data only.
    			for(int item=0; item<ListView_GetItemCount(GetDlgItem(hwThis, LIST1)); item++){
    				ListView_GetItem(GetDlgItem(hwThis, LIST1), &lvi);
    				int value = GetLstItemLong(GetDlgItem(hwThis, LIST1), item, ((NM_LISTVIEW*)lParam)->iSubItem);  // numeric sort
    				if(!bSortAscending) { value = -value; }
    				lvi.lParam = (long)&value;
    				ListView_SetItem(GetDlgItem(hwThis, LIST1), &lvi);
    			}
    			LPARAM lParamSort = 0;
    			ListView_SortItems(GetDlgItem(hwThis, LIST1), CompareFunc, lParamSort);
    			break;
    My integer sort function. Both lParam1 & 2 are 0 when i click a columnheader...What's happening here??
    Code:
    int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort){
      // First two parameters are item data to be compared.
      // Third parameter is passed from SortItems, not used here.
      if (lParam1 < lParam2) {return -1;}
      if (lParam1 > lParam2) {return  1;}
      return  0;
    }

  2. #2
    Registered User lobo's Avatar
    Join Date
    Oct 2001
    Posts
    71
    if that is exact code you are using, then problem might be that you are not setting

    lvi.mask = LVIF_PARAM;

    prior using lvi structure to set param...

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    Thanks Lobo!
    That solved the problem, except for that subitems don't display anymore...
    I'll just have to keep trying...

  4. #4
    Registered User lobo's Avatar
    Join Date
    Oct 2001
    Posts
    71
    from msdn (on list views):

    Note that sorting items does not reorder the corresponding subitems. Thus, if any subitems are not callback items, you must regenerate the subitems after you sort them.

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    Aaaargh!!
    Maybe i should start using the online version of MSDN..!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting with pointer of pointers to array
    By dunpealslyr in forum C++ Programming
    Replies: 6
    Last Post: 10-01-2007, 11:26 PM
  2. MMO Theory, How to make Gameloop & Sockets both work?
    By Zeusbwr in forum Game Programming
    Replies: 3
    Last Post: 08-01-2005, 12:29 PM
  3. Replies: 3
    Last Post: 05-07-2005, 01:52 AM
  4. The Bludstayne Open Works License
    By frenchfry164 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-26-2003, 11:05 AM
  5. how do you make the X Y thing work?
    By bluehead in forum C++ Programming
    Replies: 2
    Last Post: 11-05-2001, 04:19 PM