小弟现在CObjiect的一个派生类中建立呢一个对话框,对话框中有列表选项,现在对列表的不同列经行排序,用到下面的程序:
void CHfreqListDlg::OnColumnclickListHfreq(NMHDR* pNMHDR, LRESULT* pResult) 
{

NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;

HD_NOTIFY *phdn = (HD_NOTIFY *) pNMHDR;

static BOOL bSortAscending = TRUE;
    if( phdn->iButton > =0 )
    {    
// User clicked on header using left mouse button
        bSortAscending = !bSortAscending;
        
        int nSortedCol = phdn->iButton;
        SortTextItems( nSortedCol, bSortAscending, 0, -1 );
}BOOL CHfreqListDlg::SortTextItems(int nCol, BOOL bAscending, int low, int high)//Add by GHY 2008522
{
if( nCol >= ((CHeaderCtrl*)m_ctrlHfreqList.GetDlgItem(0))->GetItemCount() )
return FALSE;

if( high == -1 ) high = m_ctrlHfreqList.GetItemCount() - 1;

int lo = low;
int hi = high;
CString midItem;

if( hi <= lo ) return FALSE;

midItem = m_ctrlHfreqList.GetItemText( (lo+hi)/2, nCol );

// loop through the list until indices cross
while( lo <= hi )
{
// rowText will hold all column text for one row
CStringArray rowText;

// find the first element that is greater than or equal to 
// the partition element starting from the left Index.
if( bAscending )
while( ( lo < high ) && ( m_ctrlHfreqList.GetItemText(lo, nCol) < midItem ) )
++lo;
else
while( ( lo < high ) && ( m_ctrlHfreqList.GetItemText(lo, nCol) > midItem ) )
++lo;

// find an element that is smaller than or equal to 
// the partition element starting from the right Index.
if( bAscending )
while( ( hi > low ) && ( m_ctrlHfreqList.GetItemText(hi, nCol) > midItem ) )
--hi;
else
while( ( hi > low ) && ( m_ctrlHfreqList.GetItemText(hi, nCol) < midItem ) )
--hi;

// if the indexes have not crossed, swap
// and if the items are not equal
if( lo <= hi )
{
// swap only if the items are not equal
if( m_ctrlHfreqList.GetItemText(lo, nCol) != m_ctrlHfreqList.GetItemText(hi, nCol))
{
// swap the rows
LV_ITEM lvitemlo, lvitemhi;
int nColCount = ((CHeaderCtrl*)m_ctrlHfreqList.GetDlgItem(0))->GetItemCount();
rowText.SetSize( nColCount );
int i;
for( i=0; i<nColCount; i++)
rowText[i] = m_ctrlHfreqList.GetItemText(lo, i);
lvitemlo.mask = LVIF_IMAGE | LVIF_PARAM | LVIF_STATE;
lvitemlo.iItem = lo;
lvitemlo.iSubItem = 0;
lvitemlo.stateMask = LVIS_CUT | LVIS_DROPHILITED | 
LVIS_FOCUSED |  LVIS_SELECTED | 
LVIS_OVERLAYMASK | LVIS_STATEIMAGEMASK;

lvitemhi = lvitemlo;
lvitemhi.iItem = hi;

m_ctrlHfreqList.GetItem( &lvitemlo );
m_ctrlHfreqList.GetItem( &lvitemhi );

for( i=0; i<nColCount; i++)
m_ctrlHfreqList.SetItemText(lo, i, m_ctrlHfreqList.GetItemText(hi, i));

lvitemhi.iItem = lo;
m_ctrlHfreqList.SetItem( &lvitemhi );

for( i=0; i<nColCount; i++)
m_ctrlHfreqList.SetItemText(hi, i, rowText[i]);

lvitemlo.iItem = hi;
m_ctrlHfreqList.SetItem( &lvitemlo );
}

++lo;
--hi;
}
}

// If the right index has not reached the left side of array
// must now sort the left partition.
if( low < hi )
SortTextItems( nCol, bAscending , low, hi);

// If the left index has not reached the right side of array
// must now sort the right partition.
if( lo < high )
SortTextItems( nCol, bAscending , lo, high );

return TRUE;
}
现在有个问题,改排序的方程对与字符串的排序是正确的,但是换成整型的数组就不对呢,比如要对(1,15,18,2,36,4,28)这组数按从小到大排序的话,按照上面程序排序结果为(1,15,18,2,28,36,4),他把这个整型数给拆开来排序,请问对上面的程序做真样的修改能使他能够正确的排序?

解决方案 »

  1.   

     第一 从list获得数据是否正确?
    第二 你的快速排序的算是否有错?
      

  2.   

    midItem = m_ctrlHfreqList.GetItemText( (lo+hi)/2, nCol ); 
    ...
    if( bAscending ) 
    while( ( lo < high ) && ( m_ctrlHfreqList.GetItemText(lo, nCol) < midItem ) ) 
    ++lo; 
    else 
    while( ( lo < high ) && ( m_ctrlHfreqList.GetItemText(lo, nCol) > midItem ) ) 
    ++lo; 
    ...
    if( bAscending ) 
    while( ( hi > low ) && ( m_ctrlHfreqList.GetItemText(hi, nCol) > midItem ) ) 
    --hi; 
    else 
    while( ( hi > low ) && ( m_ctrlHfreqList.GetItemText(hi, nCol) < midItem ) ) 
    --hi; 这里的比较都是按字符串比较处理的,
    整数应该按整数来比较