Solved

Sorting???

Posted on 1998-11-01
4
182 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

707 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