Re: Listview subitem sorting..

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Per Rollvang (per.rollvang_at_chello.no)
Date: 03/14/04


Date: Sun, 14 Mar 2004 23:48:23 +0100


"Vas" <anonymous@discussions.microsoft.com> wrote in message
news:BD40499B-9E53-4B6B-8ADB-5D361585800B@microsoft.com...
> Yes you can sort by different columns, you have to create a class that's
derived from IComparer. Check your documentation for the ListView's class
"Sort method", not the property, for a good example.
>
> A crude example:
>
> // call to immediately sort
> listView.ListViewItemSorter = new ComparerClass(columnNumber);
>
>
> // IComparer Class
> class ComparerClass: IComparer
> {
> private int col;
>
> public ComparerClass (int column) { col = column; }
>
> public int Compare(object x, object y)
> {
> // cast items to strings
> return string.Compare(((ListViewItem)x).SubItems[col].Text,
((ListViewItem)y).SubItems[col].Text);
> }
> }
>
>
> You can add constructors to inform the sort whether its going to be
strings, ints, and other stuff.
>
Thanks a lot Vas!

I really got pushed in the right direction! If I can sit back and read a
bit, I may figure out how to sort numerics as well... ; )

-Per



Relevant Pages

  • Re: IComparable sort problem
    ... int IComparable.CompareTo ... Because if you were attempting to sort a list of points in ascending ... It would be helpful if you provided a complete IComparer ... generally on larger lists of points. ...
    (microsoft.public.dotnet.languages.csharp)
  • Access a Nested Property for a reflection based Comparer
    ... public class GenericSorter: IComparer ... String sortProperty; ... if I pass sortProperty string such as ... "thisDepartment.ID" (in order to sort by the department that the object ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Access a Nested Property for a reflection based Comparer
    ... You have to recurse, find a similar code below with that taken into account ... > public class GenericSorter: IComparer ... > String sortProperty; ... > "thisDepartment.ID" (in order to sort by the department that the object ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: System.Array.Sort on a Class Array
    ... The first hit from Google search for IComparer ... .NET will call your compare routine to ... supplying the means for .NET to determine the sort order. ...
    (microsoft.public.dotnet.general)
  • Re: User class sorting
    ... as parameter instead of using generics. ... passing it to the Sort method of your collections. ... If you want the class implementing IComparer to be generic (e.g. able ... Currently, when I need sort a collection of user class, for example: ...
    (microsoft.public.dotnet.languages.csharp)