Discussion:
ListView - sorting
(too old to reply)
David Ayre
2008-07-17 18:37:18 UTC
Permalink
Hi,

I am experimenting with the TListView component to find out what it can do and have, so far, managed to set up columns into which I have written text. If I set the sorted parameter to be Both, it
sorts the lines so that the first column is in order. Is it
possible to make it sort according to another column?

Thanks,

David
Remy Lebeau (TeamB)
2008-07-17 20:38:22 UTC
Permalink
Is it possible to make it sort according to another column?
You have to set SortType back to stNone and then use CustomSort() instead.


Gambit
David Ayre
2008-07-18 08:50:14 UTC
Permalink
Post by Remy Lebeau (TeamB)
You have to set SortType back to stNone and then use CustomSort() instead.
I have had a look at CustomSort, but find the description
hard to understand. It gives the definition as:-

bool CustomSort (int (__stdcall *)(long,long,long) SortProc, int lParam);

But doesn't clarify what everything is. Perhaps I'm a bit slow,
but perhaps you could explain how to use this function.

Thanks,

David
Remy Lebeau (TeamB)
2008-07-18 16:52:47 UTC
Permalink
Post by David Ayre
I have had a look at CustomSort, but find the description
hard to understand.
Which version of BCB are you using? The documentation in my copy of BCB 6
is very clear about it:

TCustomListView::CustomSort
Sorts the items in the list using the specified ordering function.

typedef int (CALLBACK *PFNLVCOMPARE)(LPARAM lParam1, LPARAM lParam2,
LPARAM lParam);
bool __fastcall CustomSort(PFNLVCOMPARE SortProc, int lParam);

Description
Call CustomSort to sort the items in the list using the ordering
function defined by the SortProc parameter. The SortProc parameter specifies
an ordering function that compares the list items passed as lParam1 and
lParam2. The ordering function returns an integer that indicates whether
lParam1 is the same as lParam2 (return value is 0), lParam1 is greater than
lParam2 (return value is greater than 0), or lParam1 is less than lParam2
(return value is less than 0). The lParam parameter of CustomSort is an
optional value that is passed as the third parameter to the ordering
function.

If the SortProc parameter is NULL, CustomSort compares the list items by
generating an OnCompare event. This allows the OnCompare event handler to
provide different ordering criteria (such as ascending vs. descending
order), based on the value of the lParam parameter.

If the ordering function is not provided and there is no OnCompare event
handler, CustomSort sorts items alphabetically by their Caption values.

CustomSort returns true if the list is successfully sorted.

Warning: CustomSort does not work if the application is running in
virtual mode.
Post by David Ayre
Perhaps I'm a bit slow, but perhaps you could explain how to use this
function.
int CALLBACK MySortProc(TListItem *Item1, TListItem *Item2, LPARAM
lParam)
{
if (lParam == 0 )
return AnsiCompareStr(Item1->Caption, Item2->Caption);
else
return AnsiCompareStr(Item1->SubItems->Strings[lParam-1],
Item2->SubItems->Strings[lParam-1]);
}


ListView1->CustomSort((PFNLVCOMPARE)&MySortProc, SomeColumnIndex);


Gambit
David Ayre
2008-07-19 15:19:27 UTC
Permalink
Post by Remy Lebeau (TeamB)
Which version of BCB are you using? The documentation in my copy of BCB 6
BDS2006
Post by Remy Lebeau (TeamB)
ListView1->CustomSort((PFNLVCOMPARE)&MySortProc, SomeColumnIndex);
This seems to require ListView1->CustomSort((PFNLVCOMPARE)&MySortProc(lParam,
lParam,lParam), SomeColumnIndex);
but I don't understand what to put here.
Also, how do you set the SortType to stNone as

ListView1->SortType = stNone;

Doesn't compile.

Thanks,

David
David Ayre
2008-07-19 17:48:54 UTC
Permalink
Not sure what I did wrong, but now it works.
Thanks for your help.

Still got a problen with ListView1->SortType = stNone;
However, the sorting seems to work fine if I leave it as stBoth.

Thanks,

David
Remy Lebeau (TeamB)
2008-07-20 07:15:51 UTC
Permalink
Post by David Ayre
This seems to require
ListView1->CustomSort((PFNLVCOMPARE)&MySortProc(lParam,
lParam,lParam), SomeColumnIndex);
No, it does not. You are trying to call MySortProc() and then pass the
memory address of its return value to CustomSort(). That is not correct.
You need to pass the memory address of MySortProc() itself, which
CustomSort() will then call as needed. That is what my example was doing.
Post by David Ayre
but I don't understand what to put here.
Did you try as-is what I gave you?
Post by David Ayre
Also, how do you set the SortType to stNone as
ListView1->SortType = stNone;
Doesn't compile.
ListView1->SortType = Comctrls::stNone;

Or, just set it in the Object Inspector at design-time instead.


Gambit
David Ayre
2008-07-20 16:58:31 UTC
Permalink
Thanks Remy,

All understood now and it is working OK.

Cheers,

David

Loading...