CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Guest

    Sorting Listview collumns!?

    Hi

    Could someone please post a piece of code to show how to sort strings as well as integers, depending on the collumn header clicked upon.
    Please add comments. Cut & paste from a project of yours, if not too much to ask.

    Thank you.





  2. #2
    Guest

    Re: Sorting Listview collumns!?

    LVS_SORTING. BUTTON CLICK NMHEADER *PLK;
    PLK->iButton.


  3. #3
    Guest

    Huh?

    plz Explain what you mean.

    Thank you.



  4. #4
    Guest

    Re: Sorting Listview collumns!?

    I've cut a bit out of my project for you, I hope there is enough.
    You can ignore the sort images, just remove the code.
    NB: Use the wizard to generate the OnColumnclick function.


    In header file ..........

    typedef struct
    {
    CListCtrl* pListCtrl;
    int iCol;
    BOOL bSortText;
    BOOL bAscendOrder;
    } SortInfoT;

    // Attributes
    public:
    int m_iNofCols;
    SortInfoT m_sortInfo;
    BOOL m_bColTypeText[20];
    CSortableHeaderCtrl m_ctlHeaderCtrl;

    // Operations
    public:
    static int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort);




    in cpp file ............

    void CErrorView::OnColumnclick(NMHDR* pNMHDR, LRESULT* pResult)
    {
    // Keep track of sort order and column used for sorting.
    static int s_iSortColumn = -1;
    static BOOL s_bSortAscending = TRUE;
    int idx;

    // Get the index of the column header that was clicked.
    NM_LISTVIEW* pNMListView = (NM_LISTVIEW*) pNMHDR;
    int iColumn = pNMListView -> iSubItem;
    int iItems;
    CListCtrl& ListCtrl = GetListCtrl();

    // Reverse sort order if column was just previously clicked.
    if (iColumn == s_iSortColumn)
    s_bSortAscending = !s_bSortAscending;
    else
    s_bSortAscending = TRUE;

    s_iSortColumn = iColumn; // Set item data for each row to column value,

    // since sort routine gets passed item data only.
    iItems = ListCtrl.GetItemCount();

    // Set all icons to the default
    for (idx=0; idx<m_iNofCols; idx++)
    m_ctlHeaderCtrl.SetSortImage( idx, CSortableHeaderCtrl::SORT_DIAMOND);

    if (m_sortInfo.iCol == iColumn)
    m_sortInfo.bAscendOrder = !m_sortInfo.bAscendOrder;
    else
    m_sortInfo.bAscendOrder = TRUE;

    if (m_sortInfo.bAscendOrder == TRUE)
    m_ctlHeaderCtrl.SetSortImage( iColumn, CSortableHeaderCtrl::SORT_UP );
    else
    m_ctlHeaderCtrl.SetSortImage( iColumn, CSortableHeaderCtrl::SORT_DOWN );

    m_sortInfo.iCol = iColumn;
    m_sortInfo.pListCtrl = &ListCtrl;
    m_sortInfo.bSortText = m_bColTypeText[iColumn];

    if (m_sortInfo.bSortText)
    { // Sort Text
    for (int item=0; item<iItems; item++)
    {
    ListCtrl.SetItemData(item, item);
    }
    }
    else
    { // Sort Integers
    CString sText;
    int iValue;
    for (int item=0; item<iItems; item++)
    {
    sText = ListCtrl.GetItemText(item, iColumn);
    iValue = atoi(sText); // numeric sort
    ListCtrl.SetItemData(item, iValue);
    }
    }

    ListCtrl.SortItems(CompareFunc, (DWORD)&m_sortInfo);
    }

    int CALLBACK CErrorView::CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
    { // First two parameters are item data to be compared.
    // Third parameter is sort information.

    int iItem1 = (int) lParam1;
    int iItem2 = (int) lParam2;
    SortInfoT* pSortInfo = (SortInfoT*)lParamSort;

    if (pSortInfo->bSortText)
    {
    CString s1 = pSortInfo->pListCtrl->GetItemText(iItem1, pSortInfo->iCol);
    CString s2 = pSortInfo->pListCtrl->GetItemText(iItem2, pSortInfo->iCol);


    if(pSortInfo->bAscendOrder)
    return s1.Compare(s2);
    else
    return s2.Compare(s1);
    }
    else
    { // Sort Integers
    if(pSortInfo->bAscendOrder)
    return iItem1 - iItem2;
    else
    return iItem2 - iItem1;
    }
    }

    Jeff Nuttall

    PS I don't know what happend to the tabs, but at least the line returns are there.


  5. #5
    Guest

    Thanks Jeff!



    Just bloody marvelous!

    Cheerio


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured