Link to home
Start Free TrialLog in
Avatar of kvnsdr
kvnsdr

asked on

C#: Listview column sorter, numerical Decending???

  I found a very nice MicroSoft KB Article on ListView Column Sorting thats allows clicking column headers to sort accending or decending much like native Windows Operating system Listviews do.  HOW TO: Sort a ListView Control by a Column in Visual C# .NET   http://support.microsoft.com/?kbid=319401

   However, one of my five listview columns has numbers and does not sort the way I would like. I realize when the listview is first loaded via an SQL table sort query.

Q. How can I enable a ListView to sort the text columns one way and the numreical columns another way???


Upon loading it looks like this:
150
110
100
90
8
5
2
1


After a column click: (Accending)

1
10
110
12
13
153
16
2
23
24
3

After a column click: (Decending)

3
23
24
185
17

ASKER CERTIFIED SOLUTION
Avatar of eternal_21
eternal_21

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kvnsdr
kvnsdr

ASKER

So far so good, except I get the following error on the new class:

 'MyCompare': member names cannot be the same as their enclosing type
Avatar of kvnsdr

ASKER

Sorry, my mistake..

Another error though: Input string was not in a correct format:

MyCompare (Line 17):
return Convert.ToInt32(x).CompareTo(Convert.ToInt32(y));

ListViewColumnSorter (Line 58):
compareResult = ObjectCompare.Compare(listviewX.SubItems[ColumnToSort].Text,listviewY.SubItems[ColumnToSort].Text);
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kvnsdr

ASKER

Works like a charm, thank you.........  The completed code is below:


Put this class in your project:

  public class MyCompare: System.Collections.IComparer
  {
    private System.Collections.CaseInsensitiveComparer ciComparer = new System.Collections.CaseInsensitiveComparer();
    private System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"^\d*$");    // Regex(@"\d*");

    public int Compare(object x, object y) // Assuming x, y are Strings (from ListView.Text)
    {
      if(regex.Match((string)x).Success&&regex.Match((string)y).Success)
        return Convert.ToInt32(x).CompareTo(Convert.ToInt32(y));
      else
        return ciComparer.Compare(x, y);
    }
  }

And then change the code:

  private CaseInsensitiveComparer ObjectCompare;

to:

  private MyCompare ObjectCompare;


And change:

  ObjectCompare = new CaseInsensitiveComparer();

to:

  ObjectCompare = new MyCompare();
How would you use this code in the a project.  I'm trying to sort a listview.
Avatar of kvnsdr

ASKER

I initially submitted this question regarding an article and code found at Microsoft. Go to the link below to start with, then back here to fill in a few blanks.......

  http://support.microsoft.com/?kbid=319401