Sorting in a Listview

G

Guest

My question is this:

How can I apply some kind of a function to a listview that allows it to sort
by both string and numbers whenever a header is clicked?

For example:

-------------------
INDEX
-------------------
1
-------------------
2
-------------------
3
------------------
hello
------------------
and
------------------

I want the user to be able to click the "INDEX" header and sort the
corresponding results by letters and numbers. I've searched the net for code,
but it either doesn't work or I can only click the header once and it will
not sort again.

Any suggestions?
 
G

Guest

Doesn't the listview control have a property called "Sorting" that does all
of this for you?
 
G

Guest

It has a property called "sorting", and it gives you the options of ascending
and descending.

It's not very effective though. It only sorts the first column of items
according to what you specified. Also, you cannot click on the headers to
sort the other columns.

In a normal listview in various programs, you can just click on the header
and it will reverse the sorting each time you click it
(Ascending-descending). The sorting property doesn't allow this...
 
G

Guest

I chopped together some code for you. Download ListView.zip from

http://simsof.united.net.kg/samples/

This sample has one column of numbers and a second column of strings. Click
on a column to make it the sort column. Click on it again and it will toggle
between ascending and descending orders. I just threw it together quicklike
so if there is a bug or something doesn't seem to work let me know. You will
need to make adjustments to make it work with whatever code you want because
I had to hard code when it sorted numerically and when it sorted by strings.

mosimu
 
G

Guest

The code you provided works with no problems on columns with only integers or
only strings. However, I have a column with a mixed string containing both
letters and numbers. So when I click the header of the column containing the
mixed string, I get "Input string was not in correct format" error.

Any ideas around this?

Thanks
 
G

Guest

By the the way, after the debugging, it appears this line (& probably the
next line) are the problem lines:

int ione = one->SubItems->Item[col]->Text->ToInt32(0);

Hope that helps
 
M

muchan

ReMEn said:
The code you provided works with no problems on columns with only integers or
only strings. However, I have a column with a mixed string containing both
letters and numbers. So when I click the header of the column containing the
mixed string, I get "Input string was not in correct format" error.

Any ideas around this?

Thanks

You define a compare function

=== snippet from my codes: ===
int CALLBACK ListCtrl::compareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
...
}
==============================

and call it from function reacting to LVN_COLUMNCLICK notifcation


=== snippet from my codes: ===
void ListCtrl::sortCol(int iCol) // dispatch LVN_COLUMNCLICK, iCol is ((LPNMLISTVIEW)lParam)->iSubItem
{
int param3;
...

ListView_SortItems( hwndLV, (PFNLVCOMPARE) ListCtrl::compareFunc, (LPARAM)(param3));
...
}
==============================

And it's your compare Function which decide how to sort the data.

In you case, the user interface need to remember the last option of sorting,
and decide which way to do next, and encode this info in the 3rd parameter of
the ListView_SortItems(). Your compareFunc should decode the parameter and
do (or call separate functions of) comparaison of the data.

Because these things is enough complecated, I wrote in separate ListCtrl class once
and forgot the the details for forever, happy to be a client user of my own class. :)


muchan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top