Solved

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

Posted on 2004-09-09
7
2,002 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

756 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