|
Hi,
The code has sort routines for sorting numbers, text, and date, but when I use it with IP addresses, it doesn't seem to work. For example, if I have the following IP addresses in the list:
10.10.10.1
10.10.10.2
10.10.10.3
and I enter a new address value, for example: 10.10.10.10, I get the following result:
10.10.10.1
10.10.10.10 <---new value
10.10.10.2
10.10.10.3
The current sorting routine for sorting the date is close to what I would like to do, but the difference is that, the number of digits in the 4 octets of the IP address is not of a fixed length like the date (99/99/9999), because the user can enter different values like: 1.1.1.1 or 192.254.100.244, which would change the position of the indices if we are to implement a similar routine.
How can I implement this? Any help is much appreciated.
CS
|
|
|
|
|
Ok here's code to make it sort IP Addresses. Note: this will make the code depend on the winsock libraries for inet_addr, you could re-implement inet_addr yourself if you wanted to avoid this dependencies. To the code...
You need to add these 2 functions to SortListCtrl.cpp:
bool IsIPAddr(LPCSTR pszText)
{
ASSERT_VALID_STRING( pszText );
if (inet_addr(pszText) != INADDR_NONE)
return true;
else
return false;
}
int IPAddrCompare(LPCTSTR pszIP1, LPCTSTR pszIP2)
{
ASSERT_VALID_STRING( pszIP1 );
ASSERT_VALID_STRING( pszIP2 );
long lIP1 = inet_addr(pszIP1);
long lIP2 = inet_addr(pszIP2);
if( lIP1 < lIP2 )
return -1;
if( lIP1 > lIP2 )
return 1;
return 0;
}
then you need to change the CSortListCtrl::CompareFunction function comparison part to:
if( IsNumber( pszText1 ) )
return pListCtrl->m_bSortAscending ? NumberCompare( pszText1, pszText2 ) : NumberCompare( pszText2, pszText1 );
else if (IsIPAddr(pszText1))
return pListCtrl->m_bSortAscending ? IPAddrCompare(pszText1, pszText2) : IPAddrCompare(pszText2, pszText1);
else if( IsDate( pszText1 ) )
return pListCtrl->m_bSortAscending ? DateCompare( pszText1, pszText2 ) : DateCompare( pszText2, pszText1 );
else
// text.
return pListCtrl->m_bSortAscending ? lstrcmp( pszText1, pszText2 ) : lstrcmp( pszText2, pszText1 );
|
|
|
|
|
Hi VS.NOT,
Here's a small modification to help if you have a UNICODE project.
Jeff
INT SortIPAddress( LPCTSTR pszOne, LPCTSTR pszTwo, INT nOrder )
{
INT nReturn = 0;
ULONG lIP1, lIP2;
#ifdef UNICODE
{
USES_CONVERSION;
lIP1 = inet_addr( T2A( pszOne ) );
lIP2 = inet_addr( T2A( pszTwo ) );
}
#else
{
lIP1 = inet_addr( pszOne );
lIP2 = inet_addr( pszTwo );
}
#endif
if( lIP1 < lIP2 ) {
nReturn = -1;
} else if ( lIP1 > lIP2 ) {
nReturn = 1;
} else {
nReturn = 0;
}
return nOrder == SORT_ASCENDING ? nReturn : nReturn * -1;
}
|
|
|
|
|
I found that the above sort does not work as expected! It sorts on the 4th octet, then the 1st and 2nd - disregarding the 3rd octet.
Here's something I used to work around (I did not feel like Googling for a solution). It assumes a well formed IP address of the form a.b.c.d:
INT SortIPAddress( LPCTSTR pszOne, LPCTSTR pszTwo, INT nOrder )
{
const INT NOT_FOUND = -1;
INT nReturn = 0, nPos, nShift;
CString szIP1 = pszOne, szIP2 = pszTwo;
ULONG lIP1 = 0, lIP2 = 0;
nShift = 24;
while( NOT_FOUND != ( nPos = szIP1.Find( _T(".") ) ) ) {
lIP1 += _ttoi( szIP1.Left( nPos ) ) << nShift;
szIP1 = szIP1.Right( szIP1.GetLength() - nPos - 1 );
nShift -= 8;
}
lIP1 += _ttoi( szIP1 );
nShift = 24;
while( NOT_FOUND != ( nPos = szIP2.Find( _T(".") ) ) ) {
lIP2 += _ttoi( szIP2.Left( nPos ) ) << nShift;
szIP2 = szIP2.Right( szIP2.GetLength() - nPos - 1 );
nShift -= 8;
}
lIP2 += _ttoi( szIP2 );
nReturn = lIP1 - lIP2;
return nOrder == SORT_ASCENDING ? nReturn : nReturn * -1;
}
|
|
|
|
|
if pszIP1 is host byte order, should to change to:
long lIP1 = htonl(inet_addr(pszIP1));
long lIP2 = htonl(inet_addr(pszIP2));
|
|
|
|
|
beautiful..
|
|
|
|
|
Hello,
How about sorting, when style set to Icon. Is it supported by standart CListCtrl? Or there will be no other way, but to implement it's handling myself?
Any ideas?
Thank you.
Rimantas
|
|
|
|
|
I use the sort method of CListCtrl,but the following code
is not work,Why?
static int CALLBACK MyCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
CListCtrl* pListCtrl = (CListCtrl*) lParamSort;
CString strItem1 = pListCtrl->GetItemText(lParam1, 0);
CString strItem2 = pListCtrl->GetItemText(lParam2, 0);
return strcmp(strItem2, strItem1);
}
void SortItems()
{
extern CListCtrl* pmyListCtrl;
pmyListCtrl->SortItems(MyCompareProc, (LPARAM) pmyListCtrl);
}
Thanks in advance!
Best Regards!
|
|
|
|
|
first, the second parameter of "SortItems" must be DWORD type, but in your code, the second parameter is "LPARAM" type.
second, check if the "pmyListCtrl" points to the right CListCtrl object. it's possible that "pmyListCtrl" is null.
only my opinion, it can be wrong...
|
|
|
|
|
you have to use SetItemData() to set the sort index first.
|
|
|
|
|
In order for floating point sorting to be done properly, a couple of simple changes need to be made. (Great coding, btw!):
Before:
bool IsNumber( LPCTSTR pszText )
{
ASSERT_VALID_STRING( pszText );
register int i, length = lstrlen( pszText );
for( i = 0; i < length; i++ )
if( !_istdigit( pszText[ i ] ) )
return false;
return true;
}
After:
bool IsNumber( LPCTSTR pszText )
{
ASSERT_VALID_STRING( pszText );
register int i, length = lstrlen( pszText );
for( i = 0; i < length; i++ )
if( !_istdigit( pszText[ i ] ) && (pszText[ i ] != _T( '.' )) )
return false;
return true;
}
and Before:
int NumberCompare( LPCTSTR pszNumber1, LPCTSTR pszNumber2 )
{
ASSERT_VALID_STRING( pszNumber1 );
ASSERT_VALID_STRING( pszNumber2 );
const int iNumber1 = atoi( pszNumber1 );
const int iNumber2 = atoi( pszNumber2 );
if( iNumber1 < iNumber2 )
return -1;
if( iNumber1 > iNumber2 )
return 1;
return 0;
}
After:
int NumberCompare( LPCTSTR pszNumber1, LPCTSTR pszNumber2 )
{
ASSERT_VALID_STRING( pszNumber1 );
ASSERT_VALID_STRING( pszNumber2 );
const float iNumber1 = atof( pszNumber1 );
const float iNumber2 = atof( pszNumber2 );
if( iNumber1 < iNumber2 )
return -1;
if( iNumber1 > iNumber2 )
return 1;
return 0;
}
|
|
|
|
|
I want to use this contorl in a SDI project, but when I creat this control in the view, the program got an "unknown software exception(0x800000003)". I use create function in the OnInitialUpdate(), and the code like this:
...
m_list = new CSortListCtrl;
if (!m_list) return;
CRect rect;
GetClientRect(rect);
m_list->Create(WS_CHILD|WS_VISIBLE|WS_BORDER|LVS_REPORT,rect, this, 100);
...
Can someone help me? Is there any SDI or MDI demo about this control?
|
|
|
|
|
I have the same problem too.
I need some advice.
help us.
|
|
|
|
|
I didn't see any responses to your request on the SDI example for the sort list control. Did you ever get it figured out? I'm trying to do the same thing.
Mike Ellertson
|
|
|
|
|
First place the listctrl onto a dialog, and then put the dialog into a view.
______________
Anything, I Can.
|
|
|
|
|
|
Hi,
In View, I am unable to successfully create an instance of CSortListCtrl.
The statement CSortListCtrl m_ListCtrl; or CSortListCtrl = new m_ListCtrl; returns a NULL for m_ListCtrl. So am not able to reference any of its functions in View. I wish someone could show me how!!
mad:
Pete
|
|
|
|
|
Some folks had been complaining about how horrendous the header control looked in Win XP w/ Visual Styles. I believe I have fixed this issue. I changed the function SetSortArrow in SortHeaderCtrl.cpp to the following:
<br />
void CSortHeaderCtrl::SetSortArrow(const int iSortColumn, const BOOL bSortAscending)<br />
{<br />
DWORD dwHigh, dwLow;<br />
<br />
GetCommonControlVersion(dwHigh, dwLow);<br />
<br />
if (dwHigh < 6)<br />
{<br />
HD_ITEM hditem;<br />
<br />
hditem.mask = HDI_FORMAT;<br />
VERIFY(GetItem(iSortColumn, &hditem));<br />
hditem.fmt |= HDF_OWNERDRAW;<br />
VERIFY(SetItem(iSortColumn, &hditem));<br />
<br />
Invalidate();<br />
}<br />
else<br />
{<br />
HD_ITEM hditem;<br />
<br />
hditem.mask = HDI_FORMAT;<br />
VERIFY(GetItem(iSortColumn, &hditem));<br />
<br />
if (bSortAscending)<br />
{<br />
hditem.fmt &= ~HDF_SORTDOWN;<br />
hditem.fmt |= HDF_SORTUP;<br />
}<br />
else<br />
{<br />
hditem.fmt &= ~HDF_SORTUP;<br />
hditem.fmt |= HDF_SORTDOWN;<br />
}<br />
<br />
VERIFY(SetItem(iSortColumn, &hditem));<br />
<br />
if ((m_iSortColumn > -1) && (m_iSortColumn != iSortColumn))<br />
{<br />
hditem.mask = HDI_FORMAT;<br />
VERIFY(GetItem(m_iSortColumn, &hditem));<br />
<br />
hditem.fmt &= ~HDF_SORTUP;<br />
hditem.fmt &= ~HDF_SORTDOWN;<br />
VERIFY(SetItem(m_iSortColumn, &hditem));<br />
}<br />
}<br />
<br />
m_iSortColumn = iSortColumn;<br />
m_bSortAscending = bSortAscending;<br />
}<br />
and my little function to get the common control version is also in this class (although it doesn't need to be).
<br />
bool CSortHeaderCtrl::GetCommonControlVersion(DWORD &dwHigh, DWORD &dwLow)<br />
{<br />
bool bRet = false;<br />
<br />
HMODULE hMod = LoadLibrary("COMCTL32.DLL");<br />
<br />
if (hMod != NULL)<br />
{<br />
DLLGETVERSIONPROC pDllGetVersion;<br />
pDllGetVersion = (DLLGETVERSIONPROC) GetProcAddress(hMod, "DllGetVersion");<br />
<br />
if (pDllGetVersion)<br />
{<br />
DLLVERSIONINFO dvi;<br />
HRESULT hr;<br />
<br />
ZeroMemory(&dvi, sizeof(dvi));<br />
dvi.cbSize = sizeof(dvi);<br />
<br />
hr = (*pDllGetVersion)(&dvi);<br />
<br />
if (SUCCEEDED(hr))<br />
{<br />
memcpy(&dwHigh, &dvi.dwMajorVersion, sizeof(DWORD));<br />
memcpy(&dwLow, &dvi.dwMinorVersion, sizeof(DWORD));<br />
}<br />
}<br />
<br />
FreeLibrary(hMod);<br />
}<br />
else<br />
{<br />
bRet = false;<br />
}<br />
<br />
return bRet;<br />
}<br />
Hope this helps.
Give me one more medicated peaceful moment
|
|
|
|
|
Thanks for the fix. It works for me with VS6SP5, but I am using an old version of the SDK.
So I added:
#ifndef HDF_SORTUP
#define HDF_SORTUP 0x0400
#endif
#ifndef HDF_SORTDOWN
#define HDF_SORTDOWN 0x0200
#endif
|
|
|
|
|
Thanks!I think it helps a lot ^^
|
|
|
|
|
Excellent control! But when I implemented it on my dialog OnToolTipNotify ain´t called anymore.Does anyone have any idea about this??
|
|
|
|
|
Hi,
I find one bug in my program When I use the SortItems function.
my compare function which pass into the SortItems has the following prototype:
int CALLBACK CompareListViewItems(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
When I do debugging, I find the lParam1 and lParam2 passed into the compare function are always set to 0, and never changed.
Who know what is wrong in my code? how I can fix it?
Thanks a lot!
sandy
|
|
|
|
|
This is a problem in the MSDN documentation.lParam1 and lParam2 are not the index of the items but their 32-bit associated value. another problem is even if we set the 32-bit associated value of each item to be the same as the item's index this example will still not work. This is because the items will shift positions after each call to CompareFunction(...) and the 32-bit associated values will no longer represent the item's index. I found this bug when i tried to use the CSortListCtrl in a form view.
Anyway here are some changes you could make to get it working
void CSortListCtrl::Sort( int iColumn, BOOL bAscending )
{
m_iSortColumn = iColumn;
m_bSortAscending = bAscending;
m_ctlHeader.SetSortArrow( m_iSortColumn, m_bSortAscending );
for (int i=0;i < this->GetItemCount();i++)
{
this->SetItemData(i, (DWORD)i);
}
VERIFY( SortItems( CompareFunction, (LPARAM)( this ) ));
}
Also modify CompareFunction
int CALLBACK CSortListCtrl::CompareFunction( LPARAM lParam1, LPARAM lParam2, LPARAM lParamData )
{
CSortListCtrl* pListCtrl = (CSortListCtrl*) lParamData;
CString szText1 = pListCtrl->GetItemText(lParam1, pListCtrl->m_iSortColumn);
CString szText2 = pListCtrl->GetItemText(lParam2, pListCtrl->m_iSortColumn);
LPCTSTR pszText1 = szText1.GetBuffer(szText1.GetLength());
LPCTSTR pszText2 = szText2.GetBuffer(szText2.GetLength());
ASSERT_VALID_STRING( pszText1 );
ASSERT_VALID_STRING( pszText2 );
if( IsNumber( pszText1 ) )
return pListCtrl->m_bSortAscending ? NumberCompare( pszText1, pszText2 ) : NumberCompare( pszText2, pszText1 );
else if( IsDate( pszText1 ) )
return pListCtrl->m_bSortAscending ? DateCompare( pszText1, pszText2 ) : DateCompare( pszText2, pszText1 );
else
return pListCtrl->m_bSortAscending ? lstrcmp( pszText1, pszText2 ) : lstrcmp( pszText2, pszText1 );
}
|
|
|
|
|
You have to change the IsNumber function to make sure that floating point variables are not treated as text.
Otherwise, it looks good.
A reasonable man adapts himself to the world. An unreasonable one persists, trying to adapt the world to himself. That is why all the progress in the world depends on the unreasonable men.
|
|
|
|
|
Hi,
first i'd like to say "very nice class!".
but now my question:
i've attatched a image lsit to my list by SetImageList( ... );
now i wand to add a list item by calling AddItem:
list->AddItem( _T( "text" ),
_T( "123"),
_T(symbolNumber) );
the problem: only fhe first image of the image list is shown in the list.
How can i add the ability to select which image to be displayed?!?
thx nico ...
|
|
|
|
|