Solved

Sorting???

Posted on 1998-11-01
4
183 Views
Last Modified: 2013-11-20
Dear All,

I have done a sorting of a listview by using the function
CListCtrl::SortItems();.
My listview contain 4 columns:
tel no, first name, last mail, email.

The problem I faced now is :
Many people will have the same last name/ first name.
Thus when I am doing the sorting accoring to the first name,
eg. tel no   first name    last name
    999999   Peter
    888888   Peter
    222231   Peter
    982732   Peter
    777929  Peter

The tel no will not be sorted in ascedning order.
How can I implemnt like this:
eg. tel no   first name    last name
    222231   Peter
    777929   Peter
    888888   Peter
    982732   Peter
    999999  Peter

Thanks...
0
Comment
Question by:ee_lcpaa
  • 2
  • 2
4 Comments
 
LVL 8

Accepted Solution

by:
Answers2000 earned 40 total points
ID: 1324118
You've written the Compare Function right (it's a parameter to the Sort).  Compare both the name and telephone in here.  The telephone only only matters if the name is the same

// compare names
int nCmp1 = strcmp( szName1, szName2 ) ;
if ( nCmp1 != 0 ) return nCmp1 ;

// if names the same, compare phone numbers
int nCmp2 = strcmp( szPhone1, szPhone2 ) ;
return nCmp2 ;

I would give you the complete Compare Function, but I am not sure where you are getting the names & phone numbers from (this depends on how you've implemented the code to populate the list)....post your existing compare function...and I'll tell you the mods needed if you can't work it.
0
 

Author Comment

by:ee_lcpaa
ID: 1324119
Dear Sir,

The code of my compare function is:

struct ListDataView
{
ListDataView(const DWORD dwOld, const CString &strTxt, const COleDateTime Start);
DWORD dwOldData;
CString strText;
COleDateTime BaseTime;
};
///////////////////////////////////////////////////////////////////////
void CMyFormView::Sort(int nColumnClicked)
{
      int NumItems = m_list1.GetItemCount();      
      // replace Item data with pointer to ListData structure
      DWORD dwData;
      int i;

      for (i = 0; i <NumItems; i++)      {
            dwData = m_list1.GetItemData(i); // save current data to restore it later
            CString strText = m_list1.GetItemText(i, nColumnClicked);
            m_list1.SetItemData(i, (DWORD)new ListDataView(dwData, strText,                             m_cStart));      
            }

      m_list1.SortItems(Compare, dwData);

      for(i=0; i<NumItems; i++) {
                  ListDataView* pData = (ListDataView*)(m_list1.GetItemData(i));                                          
                  m_list1.SetItemData(i, pData->dwOldData);
                  delete pData;
}

int CALLBACK CMyFormView::Compare(LPARAM lParam1, LPARAM lParam2, LPARAM lParSortAsc)
{
      CString str1 = ((ListDataView*)lParam1)->strText;
      CString str2 = ((ListDataView*)lParam2)->strText;
      return str1.CompareNoCase(str2);
}

Thus, call you tell me how to modify the program?
Thanks!!!


0
 

Author Comment

by:ee_lcpaa
ID: 1324120
Sorry, some of the code had been missing.

CMyFormView::ListDataView::ListDataView(const DWORD dwOld, const CString &strTxt, const COleDateTime Start)
{
      dwOldData = dwOld;
      strText = strTxt;
      BaseTime = Start;
}

// this variabled had been declared in the .h file.
COleDateTime m_ctStart;      //base time for sorting on 24hr clock
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1324121
Ok

1. Add variable to hold the phone number to struct ListViewData

2. modify constructor for ListView data to take phone number as a parameter

This will look something like:



     struct ListDataView
     {
     ListDataView(const DWORD dwOld, const CString &strTxt, const COleDateTime
     Start, const CString& strPhn);
     DWORD dwOldData;
     CString strText;
     COleDateTime BaseTime;
     CString strPhone ; // <--- add this
     };

CMyFormView::ListDataView::ListDataView(const DWORD dwOld, const CString
     &strTxt, const COleDateTime Start, const CString& strPhn)
     {
     dwOldData = dwOld;
     strText = strTxt;
     BaseTime = Start;
     strPhone = strPhn ;
     }

3. Add code to insert the phone data into this struct in Sort (the first loop)

or (i = 0; i <NumItems; i++) {
     dwData = m_list1.GetItemData(i); // save current data to restore it later
     CString strText = m_list1.GetItemText(i, nColumnClicked);
     CString strPhn = m_list1.GetItemText( i, 0 ) ; // 0 = phone number column ?? - if not change this line
     m_list1.SetItemData(i, (DWORD)new ListDataView(dwData,
     strText,                             m_cStart, strPhn));
     }


4. Modify the Compare function



     int CALLBACK CMyFormView::Compare(LPARAM lParam1, LPARAM lParam2,
     LPARAM lParSortAsc)
     {
     CString str1 = ((ListDataView*)lParam1)->strText;
     CString str2 = ((ListDataView*)lParam2)->strText;
     int nCmp = str1.CompareNoCase(str2);
     if ( nCmp != 0 ) return nCmp ;

     CString str3  = ((ListDataView*)lParam1)->strPhone;
     CString str4 = ((ListDataView*)lParam2)->strPhone;

     return CompareNoCase( str3, str4 ) ;
     }
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how do i create updater to My Activex application? 3 79
sum28 challenge 31 105
has77  challenge 9 71
My project did see openJDK that I installed. What could be the problem 7 122
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now