Link to home
Start Free TrialLog in
Avatar of pawel3
pawel3

asked on

ListView Sorting - CompareFunction

Hi...
I would liekt to sor the listview according to column which was pressed.
Everything is OK until the
CompareFun(LPARAM lParam1,LPARAM lParam2,LPARAM sortParm)
is hit. Neither of the LPARAM 1 or 2 pass what they suposed to. They pass a value of 0 for both and sortParam
passes the column which was pressed. Below is my source.

ItemStruct *pItem1    = (ItemStruct*) lParam1;
ItemStruct *pItem2    = (ItemStruct*) lParam2;
After this assignment is done both of these are NULL
ie: pItem1 , pItem2.

Can someone take a look and tell me what is wrong.

Thank you in Advance.
Kind Regards Paul
---------------------------------------------------------
typedef struct
{
  int nItemNo;
  char strName[110];
}ItemStruct;

int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
  int result;
  ItemStruct *pItem1    = (ItemStruct*) lParam1;
  ItemStruct *pItem2    = (ItemStruct*) lParam2;
  if(pItem1 ==  NULL || pItem2 == NULL)
     return 0;
  switch(lParamSort)
  {
    case 0:
      if(pItem1->nItemNo < pItem2->nItemNo)
         result = -1;
      else
      if(pItem1->nItemNo == pItem2->nItemNo)
         result = 0;
      else
         result = 1;
      break;

      case 1:
        result = strcmp(pItem1->strName,pItem2->strName);
        break;

      default:
        result = 0;
        break;
  }
  return result;
}

void OnColumnclickList1(HWND hWnd,LPARAM lParam)
{
    HWND lWnd;
    int nColumn;
    NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)lParam;
     lWnd = GetDlgItem(hWnd,ID_SEARCHLIST);
    nColumn = pNMListView->iSubItem;
    ListView_SortItems(lWnd,CompareFunc,nColumn);
}

LRESULT SListViewNotify(HWND hWnd,LPARAM lParam)
{
   LPNMHDR  lpnmh = (LPNMHDR) lParam;
   if(search_set == 0)
      return 0;
   switch(lpnmh->code)
   {
      case LVN_COLUMNCLICK:
         OnColumnclickList1(hWnd,lParam);
         break;
   }
   return 0;
}

---------------------------------------------------------
ASKER CERTIFIED SOLUTION
Avatar of NickRepin
NickRepin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of robpitt
robpitt

Question:
In your OnColumnclickList1() what is the ID_SEARCHLIST thing all about?


Also be sure to check that your actually setting the lParam correctly in the first place. I.e. check that the LVITEM mask member includes LVIF_PARAM as well as setting the lParam member to your structure address.

Rob
PS Ignore my first question about ID_SEARCHLIST - I was just being dim.
can u post the code that uses LVM_INSERTITEM?
Avatar of pawel3

ASKER

thank you NickRepin
you suggestion worked fine
if so then could you accept my comment as an answer?