Solved

Sorting???

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Add content to output file 4 67
List out all word 7 318
Annoying "thing" blocks my view 4 115
Way to decrease size of apk file 9 87
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

828 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