Solved

Sorting in OWL implementation of ListView

Posted on 1998-08-14
7
370 Views
Last Modified: 2008-02-01
Hi
I've a problem with sorting in OWL TListView. I've written my custom compare function. When I'm calling ListView->SortItems function for the first time, everything is OK. Items are sorted correctly. Buf the second call of SortItems func. gives items back to the state after initialization (same order as it was after adding in SetupWindow()). What's wrong ?
I'm using Borland BC5.02 compiler under WIN NT 4.0 Workstation.
0
Comment
Question by:petasf
  • 4
  • 3
7 Comments
 

Author Comment

by:petasf
ID: 1170274
Adjusted points to 100
0
 
LVL 6

Expert Comment

by:snoegler
ID: 1170275
Could you show your custom compare function?
0
 

Author Comment

by:petasf
ID: 1170276
int CALLBACK CompareProc(LPARAM lparam1, LPARAM lparam2, LPARAM lparamSort)
{
int result;
char buf1[50];
char buf2[50];

   CmpStruct *cs = (CmpStruct*)lparamSort;

   cs->lv->GetItemText((int)lparam1,cs->col,buf1,49);
   cs->lv->GetItemText((int)lparam2,cs->col,buf2,49);

   result = lstrcmpi(buf1,buf2);

   return(result);
}

0
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.

 
LVL 6

Expert Comment

by:snoegler
ID: 1170277
I don't know the OWL very well but i would guess that it could help to reset the contents first
and call the SortItems() function after that.
Perhaps the SortItems() function sort relatively - so that the first call sorts the items, the next
call reverses the sort again, and so on ... (just guessing)
Do you have the source code of this SortItems() function - if yes, you could post it here to
let us take a look on it.
0
 

Author Comment

by:petasf
ID: 1170278
Yes it looks like you've said. - the naxt call the SortItems() really reverses order of items. But in my opinion it should work this way. The first call sorts items for example in the ascent order (it depends on your custom compare function, of course), the next call reverses sort, so after sort, items are in descent order. But it doesn't work like this.The next call of SortItems() gives items back to the state after initialization -my items are not sorted after initialization.
Here is source code of OWL::TListWindow::SortItems()
BUT I'M USING THE CALL OF THE WIN32 MACRO ListView_SortItems

BOOL ListView_SortItems(
    HWND hwnd,       
    PFNLVCOMPARE pfnCompare,       
    LPARAM lParamSort      
   );



//
// Sort the items within the List Window.
// TLwComparator is the base class for the comparison function
//
bool TListWindow::SortItems(const TLwComparator& comparator, uint32 lParam)
{
  TListCompareThunk ct;
  ct.This = &comparator;
  ct.ItemData = lParam;
  return ToBool(SendMessage(LVM_SORTITEMS, TParam1(&ct), TParam2(OwlListViewCompare)));
}


0
 
LVL 6

Accepted Solution

by:
snoegler earned 100 total points
ID: 1170279
I think i got the solution :)
As you know, the comparison function are only given the lParam members of the both TV_ITEM
structures to compare. So the easiest solution - and i think you did that - is to set them equal
to the index of the TV_ITEM, because that's the value you need to get the texts of the items to
compare.
Now imagine that: If the comparison function returns a  '-1', this means for the sort function
to swap the both TV_ITEMs. So also the lParam members are swapped - do you know what
i mean?
The first call to the SortItems() function mixes your lParam's which were intended as an
index - and unfortunately they are set in a way so that the next sort function will reverse the
former result exactly to what it was before.
(because the sort order is totally wrong - you see that with your debugger)

I would suggest that you set the lParam member to the index of the TV_ITEM structure again
(for each item), so that the order is correct again and then it should work.
Hope that helped ;)

0
 

Author Comment

by:petasf
ID: 1170280
Thanks Snoegler
Functionality of ListView makes me sometimes crazy.
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
MSVCR80.dll crash 2 134
How do I open an Access Data base with a password set using C++ 2 110
typedef std::deque<BYTE> ByteVector is broken in vs2012 23 82
What is sub-make ? 2 40
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

863 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

23 Experts available now in Live!

Get 1:1 Help Now