Solved

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

Posted on 2004-09-09
7
2,026 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

617 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