Solved

Sorting???

Posted on 1998-11-01
Medium Priority
193 Views
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
Question by:ee_lcpaa
• 2
• 2

LVL 8

Accepted Solution

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

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

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

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

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Suggested Courses
Course of the Month9 days, 22 hours left to enroll