CMainFrame* cFrm = (CMainFrame*)::AfxGetMainWnd();
CString strValue1 = cFrm->mcListCtrl.GetItemText(int(lParam1),cFrm->Column);
CString strValue2 = cFrm->mcListCtrl.GetItemText(int(lParam2),cFrm->Column);
int a = atoi(strValue1);
int b = atoi(strValue2);
if( a==b ) return 0;
return a>b? 1: -1;我跟踪了排序,它得到了:703》558》400》92但是,显示的时候却变成了,703在前,400随后,然后是92,最后是558,怎么回事?

解决方案 »

  1.   

    看不明白你说的,不过是否ListCtrl有 Sort风格
      

  2.   

    mcListCtrl.Create( WS_CHILD | WS_VISIBLE | LVS_REPORT | LVS_SORTASCENDING, 
    CRect(22,42,624,121), (CWnd*)&DialogBar, IDC_LIST1); 
    mcListCtrl.ModifyStyleEx(0, WS_EX_CLIENTEDGE, SWP_DRAWFRAME);而且每次排序最后的结果很可能不同。
      

  3.   

    函数如下
    int CALLBACK ListCompare(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
    {
    CMainFrame* cFrm = (CMainFrame*)::AfxGetMainWnd();
    CString strValue1 = cFrm->mcListCtrl.GetItemText(int(lParam1),cFrm->Column);
    CString strValue2 = cFrm->mcListCtrl.GetItemText(int(lParam2),cFrm->Column);
    int a = atoi(strValue1);
    int b = atoi(strValue2);
    if( a==b ) return 0;
    return a>b? 1: -1;
    }
      

  4.   

    你全写错啦!!!MSDN里的例子也是错的(误导)!!!
    请参阅:http://www.codeproject.com/buglist/listcontrolsortitemsbug.asp#xx503965xx//******************************************
    Example of CListCtrl::SortItems(...) in MSDN
    By Ivor S. Sargoytchev The given example in the documentation of CListCtrl::SortItems(...) shows us exactly the WRONG way of using the function.  
    //******************************************int CALLBACK CSCListCtrl::CompareFunction( LPARAM lParam1, LPARAM lParam2, LPARAM lParamData )

    CSCListCtrl* pListCtrl = (CSCListCtrl*) lParamData;LVFINDINFO pInfo1, pInfo2;
    pInfo1.flags = LVFI_PARAM;
    pInfo2.flags = LVFI_PARAM;
    pInfo1.lParam = lParam1;
    pInfo2.lParam = lParam2;
    int ind1 = pListCtrl->FindItem(&pInfo1);
    int ind2 = pListCtrl->FindItem(&pInfo2);
    CString strItem1 = pListCtrl->GetItemText(ind1, pListCtrl->m_iSortColumn);
    CString strItem2 = pListCtrl->GetItemText(ind2, pListCtrl->m_iSortColumn);return pListCtrl->m_bSortAscending ? lstrcmp( strItem1, strItem2 ) : lstrcmp( strItem2, strItem1 );
    }