title: C++ WinAPI - ListView sort
body: <p>I'm trying to do sort in a ListView. Didn't found any complete example how to do it, only a pieces of code.
  I've already created ListView, added columns and items, specified my Comp function. However my problem is that lp1 and lp2 are always 0. I've checked documentation and it says : "These are the values that were specified in the lParam member of the items' LVITEM structure when they were inserted into the list." which I do set.
  Here is the code :</p>
  
  <pre><code>#include &lt;windows.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;commctrl.h&gt;
  #pragma comment(lib,"comctl32.lib")
  
  HWND ListView;
  HWND hwnd;
  LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
  
  int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow)
  {
      MSG messages;
      WNDCLASSEX wincl = { 0 };
  
      wincl.lpszClassName = "Testtttt";
      wincl.lpfnWndProc = WindowProc;
      wincl.style = CS_DBLCLKS;
      wincl.cbSize = sizeof(WNDCLASSEX);
  
      wincl.hIcon = 0;
      wincl.hIconSm = 0;
      wincl.hCursor = LoadCursor(NULL, IDC_ARROW);
      wincl.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(0,0,0));// COLOR_BACKGROUND;
      wincl.hInstance = GetModuleHandle(NULL);
  
      RegisterClassEx(&amp;wincl);
  
      hwnd = CreateWindowEx(
          0,
          wincl.lpszClassName,
          "Test",
          WS_SYSMENU | WS_EX_STATICEDGE | WS_VISIBLE,
          CW_USEDEFAULT,
          CW_USEDEFAULT,
          1000,
          500,
          hwnd,
          NULL,
          GetModuleHandle(NULL),
          NULL);
  
      RECT rec;
      GetClientRect(hwnd, &amp;rec);
      SetWindowPos(ListView, 0, 0, 0, rec.right, rec.bottom - 23, 0);
  
      ShowWindow(hwnd, nCmdShow);
  
      while (GetMessage(&amp;messages, NULL, 0, 0))
      {
          TranslateMessage(&amp;messages);
          DispatchMessage(&amp;messages);
      }
  }
  
  void AddListViewItem(char item, char *text)
  {
      static int i;
      LVITEM lis;
      lis.mask = LVIF_TEXT /*| LVIF_PARAM*/;
      lis.pszText = (LPSTR)(text);
      lis.iItem = i;
      lis.iSubItem = item;
      lis.lParam = i;
      if (item==1)
      i++;
      if (item == 0)
          ListView_InsertItem(ListView, &amp;lis);
      else
          SendMessage(ListView, LVM_SETITEM, 0, (LPARAM)&amp;lis);
  }
  
  int CALLBACK myCompFunc(LPARAM lp1, LPARAM lp2, LPARAM sortParam)
  {
      bool isAsc = (sortParam &gt; 0);
      int column = abs(sortParam) - 1;
  
      char tmp[128];
      sprintf_s(tmp,sizeof tmp, "%d %d %d", lp1, lp2, column);
      MessageBox(0, (LPCSTR)tmp, "myCompFunc", MB_OK);
      return 0;
  }
  
  LRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  {
      if (message == WM_NOTIFY)
      {
          LPNMHDR lpnmh = (LPNMHDR)lParam;
          if (lpnmh-&gt;idFrom == 8553)
          if (lpnmh-&gt;code == LVN_COLUMNCLICK)
          {
              NMLISTVIEW*    pListView = (NMLISTVIEW*)lParam;
              ListView_SortItems(ListView, myCompFunc, pListView-&gt;iSubItem);
          }
      }
      else if (message == WM_CREATE)
      {
          ListView = CreateWindow(WC_LISTVIEW, (LPCSTR)L"", (WS_CHILD | WS_VISIBLE | LVS_REPORT), 0, 0, 900, 500, hwnd, (HMENU)8553, GetModuleHandle(NULL), NULL);
          SendMessage(ListView, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT); // Set style
  
          LVCOLUMN listColumn = { 0 };
          listColumn.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
          listColumn.pszText = "Column1";
          listColumn.cx = 150;
          listColumn.fmt = LVCFMT_LEFT;
          ListView_InsertColumn(ListView, 0, &amp;listColumn);
          listColumn.pszText = "Column2";
          listColumn.cx = 150;
          listColumn.iSubItem = 1;
          ListView_InsertColumn(ListView, 1, &amp;listColumn);
  
          {
              AddListViewItem(0, "a");
              AddListViewItem(1, "a 1");
  
              AddListViewItem(0, "b");
              AddListViewItem(1, "b 1");
  
              AddListViewItem(0, "c");
              AddListViewItem(1, "c 1");
          }
      }
      else if (message == WM_DESTROY)
      {
          PostQuitMessage(0);
      }
      else
          return DefWindowProc(hwnd, message, wParam, lParam);
      return DefWindowProc(hwnd, message, wParam, lParam);
      return 0;
  }
  </code></pre>