В этой программе winapi я сортирую все элементы на основе столбца «Дата». Однако он сортируется по столбцу «Описание» вместо столбца «Дата». Сортировка ListView по неправильному столбцу
Вот код в WM_NOTIFY:
static char szText[10];
NM_LISTVIEW *pNm = (NM_LISTVIEW *)lParam;
switch (((LPNMHDR)lParam)->code) {
case LVN_COLUMNCLICK:
if (pNm->iSubItem == 2)
if (ListView_SortItems(pNm->hdr.hwndFrom, CompareFunc,
(LPARAM) (pNm->iSubItem)) == FALSE)
MessageBox(hwnd, "FALSE", "", MB_OK);
break;
/* other WM_NOTIFY code */
}
ListView_SortItems
возвращает TRUE, странно. Вот функция CompareFunc:
int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
if (lParamSort == 2) {
date d1, d2; // app-defined "date" class
char b1[32], b2[32];
sscanf((char *) lParam1, "%s %d %d", b1, &d1.day, &d1.yr);
sscanf((char *) lParam2, "%s %d %d", b2, &d2.day, &d2.yr);
d1.month = monthtoi(b1); // converts month as string to number
d2.month = monthtoi(b2);
if (d1 > d2) // overloading the ">" and "<" operators
return 1;
else if (d1 < d2)
return -1;
return 0;
}
}
Я попытался проверить iSubItem против 3, а не 2 (1 на основе против 0 на основе), но это не сработало. Что я делаю неправильно?
Edit:
int monthtoi(char *s)
{
int i;
for (i = 0; i < 12; ++i) {
// MONTHS is a global array of char *, containing the months
if (strcmp(MONTHS[i], s) == 0)
return i;
}
return -1;
}
bool date::operator>(const date &x)
{
switch (this->cmp(x)) { // cmp is a private member function
case 0:
case -1:
return false;
case 1:
return true;
}
return false;
}
bool date::operator<(const date &x)
{
switch (this->cmp(x)) {
case 0:
case 1:
return false;
case -1:
return true;
}
return false;
}
int date::cmp(const date &x)
{
if (this->yr > x.yr)
return 1;
else if (this->yr < x.yr)
return -1;
if (this->yr == x.yr) {
if (this->month > x.month)
return 1;
else if (this->month < x.month)
return -1;
else if (this->day > x.day)
return 1;
else if (this->day < x.day)
return -1;
else
return 0;
}
return 0;
}
Поскольку мы не можем увидеть реализацию 'monthtoi' или ваш' оператор> 'реализацию, есть немного можно ожидать. Мы также не можем видеть ваши «LVITEM». Правильно ли настроены члены 'lParam'? – IInspectable
Изменен ответ @Inpectable – stackptr