void CFastGrid::SortList(int iSortAsInt64) { if( iSortAsInt64 == 0 ) SortTextItems( nSortedCol, bSortAscending ); else SortIntItems( nSortedCol, bSortAscending ); }
void CFastGrid::OnHeaderClicked(NMHDR *pNMHDR, LRESULT *pResult) { HD_NOTIFY *phdn = (HD_NOTIFY *) pNMHDR; if( phdn->iButton == 0 ) { // User clicked on header using left mouse button if( phdn->iItem == nSortedCol ) bSortAscending = !bSortAscending; else bSortAscending = TRUE; nSortedCol = phdn->iItem; SortTextItems( nSortedCol, bSortAscending ); // SortItems(CompareProc, (LPARAM)this); } // AfxMessageBox("YYYYW"); *pResult = 0; }
void CCJListCtrl::OnItemclick(NMHDR* pNMHDR, LRESULT* pResult) { HD_NOTIFY *phdn = (HD_NOTIFY *) pNMHDR; if( phdn->iButton == 0 ) { // User clicked on header using left mouse button if( phdn->iItem == m_nSortedCol ) m_bSortAscending = !m_bSortAscending; else m_bSortAscending = TRUE; m_nSortedCol = phdn->iItem; SortTextItems( m_nSortedCol, m_bSortAscending ); m_headerCtrl.SetSortImage(m_nSortedCol, m_bSortAscending); } *pResult = 0; }
BOOL CCJListCtrl::SortTextItems( int nCol, BOOL bAscending, int low /*= 0*/, int high /*= -1*/ ) { if( nCol >= ((CHeaderCtrl*)GetDlgItem(0))->GetItemCount() ) return FALSE; if( high == -1 ) high = GetItemCount() - 1; int lo = low; int hi = high; CString midItem; if( hi <= lo ) return FALSE; midItem = 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 ) && ( GetItemText(lo, nCol) < midItem ) ) ++lo; } else { while( ( lo < high ) && ( 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 ) && ( GetItemText(hi, nCol) > midItem ) ) --hi; } else { while( ( hi > low ) && ( 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( GetItemText(lo, nCol) != GetItemText(hi, nCol)) { // swap the rows LV_ITEM lvitemlo, lvitemhi; int nColCount = ((CHeaderCtrl*)GetDlgItem(0))->GetItemCount(); rowText.SetSize( nColCount ); int i; for( i=0; i<nColCount; i++) rowText[i] = 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; GetItem( &lvitemlo ); GetItem( &lvitemhi ); for( i=0; i<nColCount; i++) SetItemText(lo, i, GetItemText(hi, i)); lvitemhi.iItem = lo; SetItem( &lvitemhi ); for( i=0; i<nColCount; i++) SetItemText(hi, i, rowText[i]); lvitemlo.iItem = hi; 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; }
int ColumnSort(CListCtrl& list, int iSubItem, // column number (0..n-1) or -1=> last column UINT state) // CS_SORT : sort in the same order // CS_REVERSE : sort reverse // CS_FOR, CS_BACK : sort command // CS_SELVIS : 0=> show the selected item { // CS_SORTICON : icon display CHeaderCtrl *pHeader = list.GetHeaderCtrl(); int ix = pHeader->GetItemCount(); HDITEM hi; if (iSubItem >= ix) iSubItem = -1; // search the old sorted column hi.mask = HDI_IMAGE|HDI_FORMAT|HDI_LPARAM; while (--ix >= 0) { pHeader->GetItem(ix, &hi); if (hi.iImage == 0) continue; if (iSubItem >= 0 && ix != iSubItem) { // delete old display icon hi.iImage = 0; // hi.lParam |= STI_REVERSE; // ******************* hi.fmt &= ~HDF_IMAGE; pHeader->SetItem(ix, &hi); } break; } if (iSubItem < 0) { if (ix < 0) return -3; // a sorted column is not define iSubItem = ix; // there is the old column } // set the new display icon pHeader->GetItem(iSubItem, &hi); if (hi.lParam == 0) return -2; // sort disable for this column switch (state & 0x0003) { case CS_SORT : break; // in the same order case CS_REVERSE: hi.lParam ^= 1; break; // in the revers order case CS_ASCEND : hi.lParam &= ~1; break; // to impose ascending order case CS_DESCEND: hi.lParam |= 1; break; // to inpose descending order } hi.iImage = hi.lParam; if (state & CS_SORTICON) hi.fmt |= HDF_IMAGE; // the icon pHeader->SetItem(iSubItem, &hi); SortTextItems(list, iSubItem, short(hi.lParam)); if ((state & CS_SELVISIBLE) == 0) { // Make that the selected item continue to stay visible POSITION pos = list.GetFirstSelectedItemPosition(); if (pos) { // search the seleted item int no = list.GetNextSelectedItem(pos); list.EnsureVisible(no, FALSE); return no; // selected item } } return -1; }
BOOL SortTextItems(CListCtrl& list, int nCol, short mode, int low, int high) { int nColCount = list.GetHeaderCtrl()->GetItemCount(); if (high == -1) high = list.GetItemCount() - 1; int lo = low; int hi = high; CString midItem; if ( hi <= lo || hi >= list.GetItemCount()) return FALSE; midItem = list.GetItemText( (lo + hi) / 2, nCol ); double midItemDbl = atofs(midItem); long midItemLg = atodx(midItem); // loop through the list until indices cross while ( lo <= hi ) { // rowText will hold all column text for one row switch ( mode ) { case STI_TEXT|STI_REVERSE: // text ascendant // find the first element that is greater than or equal to // the partition element starting from the left Index. while ( ( lo < high ) && ( midItem.CollateNoCase(list.GetItemText(lo, nCol)) > 0 ) ) ++lo; // find an element that is smaller than or equal to // the partition element starting from the right Index. while ( ( hi > low ) && ( midItem.CollateNoCase(list.GetItemText(hi, nCol)) < 0 ) ) --hi; break; case STI_TEXT: // text descendant while ( ( lo < high ) && ( midItem.CollateNoCase(list.GetItemText(lo, nCol)) < 0 ) ) ++lo; while ( ( hi > low ) && ( midItem.CollateNoCase(list.GetItemText(hi, nCol)) > 0 ) ) --hi; break; // Trie a l'envers les numeriques (par defaut le plus grand au debut) case STI_NUMBER: // number descendant while ( ( lo < high ) && ( midItemDbl < atofs(list.GetItemText(lo, nCol))) ) ++lo; while ( ( hi > low ) && ( midItemDbl > atofs(list.GetItemText(hi, nCol))) ) --hi; break; case STI_NUMBER|STI_REVERSE: // number ascendant while ( ( lo < high ) && ( midItemDbl > atofs(list.GetItemText(lo, nCol))) ) ++lo; while ( ( hi > low ) && ( midItemDbl < atofs(list.GetItemText(hi, nCol))) ) --hi; break; case STI_HEXADEC: // number descendant while ( ( lo < high ) && ( midItemLg < atodx(list.GetItemText(lo, nCol))) ) ++lo; while ( ( hi > low ) && ( midItemLg > atodx(list.GetItemText(hi, nCol))) ) --hi; break; case STI_HEXADEC|STI_REVERSE: // number ascendant while ( ( lo < high ) && ( midItemLg > atodx(list.GetItemText(lo, nCol))) ) ++lo; while ( ( hi > low ) && ( midItemLg < atodx(list.GetItemText(hi, nCol))) ) --hi; break; // Trie a l'envers les numeriques (par defaut le plus grand au debut) case STI_ABSOLU: // number descendant while ( ( lo < high ) && ( midItemDbl < atofsa(list.GetItemText(lo, nCol))) ) ++lo; while ( ( hi > low ) && ( midItemDbl > atofsa(list.GetItemText(hi, nCol))) ) --hi; break; case STI_ABSOLU|STI_REVERSE: // number ascendant while ( ( lo < high ) && ( midItemDbl > atofsa(list.GetItemText(lo, nCol))) ) ++lo; while ( ( hi > low ) && ( midItemDbl < atofsa(list.GetItemText(hi, nCol))) ) --hi; break; default: return FALSE; } // 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 ( list.GetItemText(lo, nCol).CollateNoCase(list.GetItemText(hi, nCol)) != 0) { // swap the rows LV_ITEM lvitemlo, lvitemhi; lvitemlo.mask = LVIF_IMAGE|LVIF_STATE|LVIF_PARAM; lvitemlo.iSubItem = 0; lvitemlo.stateMask = -1; lvitemhi = lvitemlo; lvitemlo.iItem = lo; lvitemhi.iItem = hi; list.GetItem( &lvitemhi ); list.GetItem( &lvitemlo ); lvitemhi.iItem = lo; lvitemlo.iItem = hi; list.SetItem( &lvitemhi ); list.SetItem( &lvitemlo ); for (int i = 0; i < nColCount; i++) { CString &text = list.GetItemText(lo, i); list.SetItemText(lo, i, list.GetItemText(hi, i)); list.SetItemText(hi, i, text); } } ++lo; --hi; } } // If the right index has not reached the left side of array // must now sort the left partition. if ( low < hi ) SortTextItems( list, nCol, mode, low, hi); // If the left index has not reached the right side of array // must now sort the right partition. if ( lo < high ) SortTextItems( list, nCol, mode, lo, high ); return TRUE; }
BOOL CBGridCtrl::SortTextItems(int nCol, BOOL bAscending, int low, int high) { if (nCol >= GetColumnCount()) return FALSE;//大图象列表 if (high == -1) high =m_nRealRow-1; int lo = low; int hi = high; if( hi <= lo ) return FALSE; double niMid=GetItemTextV((lo+hi)/2, nCol); double nistr; while( lo <= hi ) { if( bAscending ) { nistr=GetItemTextV(lo, nCol); while (lo < high && nistr<niMid) { ++lo; nistr=GetItemTextV(lo, nCol); } } else { nistr=GetItemTextV(lo, nCol); while ((lo < high && nistr>niMid)) { ++lo; nistr=GetItemTextV(lo, nCol); } } if( bAscending ) { nistr=GetItemTextV(hi, nCol); while (hi > low && nistr>niMid) { --hi; nistr=GetItemTextV(hi, nCol); } } else { nistr=GetItemTextV(hi,nCol); while (hi > low && nistr<niMid) { --hi; nistr=GetItemTextV(hi, nCol); } } if (lo <= hi) { if (GetItemTextV(lo, nCol) != GetItemTextV(hi, nCol)) { SwapMx(m_pDoc->m_pStockBigBillData->m_pMapData[lo -1],m_pDoc->m_pStockBigBillData->m_pMapData[hi -1] ); UINT nRowHeight = m_arRowHeights[lo]; m_arRowHeights[lo] = m_arRowHeights[hi]; m_arRowHeights[hi] = nRowHeight; } ++lo; --hi; } } if( low < hi )// This code may be used in compiled form in any way you SortTextItems(nCol, bAscending, low, hi); if( lo < high ) SortTextItems(nCol, bAscending, lo, high); return TRUE; }