Sort ListView By Arbitrary Column? 
Author Message
 Sort ListView By Arbitrary Column?

I can't seem to figure out how to sort a ListView by anything but the first
column.  I don't seem to see a way to set this property in the ListView or
the ListView.Columns objects.  It seems like one way would be to swap the
column you want to sort by and the first column, sort the list, then swap
them back, but that doesn't seem like it should be necessairy.

Thanks Michael



Tue, 06 Apr 2004 23:28:09 GMT  
 Sort ListView By Arbitrary Column?
Hi Michael,


First of all you should have class implementing IComparer interface. Then
you create an instance of this class and set instance reference to
ListViewItemSorter property of listview.

When listview will sort items it will call Compare method of your class
passing two ListViewItems. All you have to do inside Compare method is to
compare :-) and return 0 if items are equal, -1 if item1 is less than item 2
or 1 if item1 is greater than item 2.

Following is a sample of this, assuming SubItem[1] is of String type:
        public class ListViewComparer : IComparer
        {
                public int Compare( object item1, object item2 )
                {
                        if( !(item1 is ListViewItem) || !(item2 is ListViewItem) )
                                return 0; // We don't know type - so both objects are same for us

                        ListViewItem lvi1 = (ListViewItem) item1;
                        ListViewItem lvi2 = (ListViewItem) item2;

                        return String.Compare( lvi1.SubItems[1].ToString(),
(string)lvi2.SubItems[1].ToString() );
                }
        }

---------------------
This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. Copyright 2001 Microsoft Corporation. All
rights reserved.



Sun, 11 Apr 2004 13:45:11 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. ListView Sorting by Column

2. Sorting a ListView by column?

3. Sorting multi column ListView -code-Any better ideas?

4. ListView.Sort() restores columns order

5. Listview Sorting by Columns

6. how to sorting listview columns?

7. Sorting ListView Box Columns

8. Sorting ListView Columns

9. sorting a listview based on column

10. example: sorting listview columns

11. sorting columns of a listview

12. ListView: How to sort the 2nd column ?

 

 
Powered by phpBB® Forum Software