Solved

# ListView Sorting - CompareFunction

Posted on 2001-07-26
Medium Priority
715 Views
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.

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;
}

---------------------------------------------------------
0
Question by:pawel3
[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

LVL 15

Accepted Solution

NickRepin earned 800 total points
ID: 6324782
<<The lParam1 parameter is the 32-bit value associated with the first item being compared, and the lParam2 parameter is the value associated with the second item.>>

You have to explicitely assign a certain value to lParam for each listview item when you create a listview.

By default, lParam contains zero.

lParam is among other members in LVITEM structure.

0

LVL 5

Expert Comment

ID: 6324802
Question:

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
0

LVL 5

Expert Comment

ID: 6326006
PS Ignore my first question about ID_SEARCHLIST - I was just being dim.
0

Expert Comment

ID: 6372658
can u post the code that uses LVM_INSERTITEM?
0

Author Comment

ID: 6681287
thank you NickRepin
you suggestion worked fine
0

LVL 15

Expert Comment

ID: 6682531
if so then could you accept my comment as an answer?
0

## Featured Post

Question has a verified solution.

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

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
###### Suggested Courses
Course of the Month15 days, 8 hours left to enroll