Solved

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

Posted on 2004-09-09
7
1,974 Views
Last Modified: 2008-01-09
  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

0
Comment
Question by:kvnsdr
  • 4
  • 2
7 Comments
 
LVL 10

Accepted Solution

by:
eternal_21 earned 125 total points
ID: 12019660
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*");

    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();
0
 
LVL 1

Author Comment

by:kvnsdr
ID: 12020458
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
0
 
LVL 1

Author Comment

by:kvnsdr
ID: 12020676
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);
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 10

Assisted Solution

by:eternal_21
eternal_21 earned 125 total points
ID: 12020846
Sorry, kvnsdr

Change Regex(@"\d*"), to Regex(@"^\d*$")... I made a little mistake there!
0
 
LVL 1

Author Comment

by:kvnsdr
ID: 12029834
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();
0
 

Expert Comment

by:cjsteynberg
ID: 12378531
How would you use this code in the a project.  I'm trying to sort a listview.
0
 
LVL 1

Author Comment

by:kvnsdr
ID: 12380689
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
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now