Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > C/C++: Общие вопросы > Вопрос по сортировке в ListCtrl.


Автор: AndreySol 19.6.2004, 16:43
Вопрос по сортировке в ListCtrl.
Помогите начинающему в Visual C++ и MFC.
Люди, дайте кто-нибудь реальный пример кода, который бы сортировал итемы ListCtrl'а по алфавиту - нашел несколько примеров на разных форумах, смотрел в MSDN но не смог получить нормальную сортировку.
Заранее спасибо за ответы и подсказки.

Автор: Baa 19.6.2004, 23:13
Вот из MSDN. Чего конкретно не понятно?
Код

The comparison function must return a negative value if the first item should precede the second, a positive value if the first item should follow the second, or zero if the two items are equivalent.

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. These are the values that were specified in the lParam member of the items' LVITEM structure when they were inserted into the list. The lParamSort parameter is the same as the dwData value.

// Sort the item in reverse alphabetical order.
static int CALLBACK
MyCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
  // lParamSort contains a pointer to the list view control.
  CListCtrl* pListCtrl = (CListCtrl*) lParamSort;
  CString    strItem1 = pListCtrl->GetItemText(lParam1, 0);
  CString    strItem2 = pListCtrl->GetItemText(lParam2, 0);

  return strcmp(strItem2, strItem1);
}

void snip_CListCtrl_SortItems()
{
  // The pointer to my list view control.
  extern CListCtrl* pmyListCtrl;

  // Sort the list view items using my callback procedure.
  pmyListCtrl->SortItems(MyCompareProc, (LPARAM) pmyListCtrl);
}


Автор: AndreySol 20.6.2004, 20:38
И этот пример я пробовал - не сортируются у меня итемы в алфавитном порядке.

Автор: Олег М 21.6.2004, 05:28
Должны сортироваться. Дело наверное в чём то-другом. Посмотри в дебагере как работает функция MyCompareProc и что возвращает

Автор: AndreySol 26.8.2004, 05:34
Накачал из сети столько по сортировке ! Буду разгребать.
Пока Всем спасибо.

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)