?
Solved

Sorting a ListView or TreeView by number and text

Posted on 2004-09-14
3
Medium Priority
?
306 Views
Last Modified: 2008-02-01
 I've for a listview and a treeview control on a form that I want to sort by number and text.  For example, it should sort this way: 1, 2, 3, 9, 10, 11, 99, 100, 101, Alpha, Beta, etc...  instead of 1, 10, 100, 101, 11, 2, 3, 9, Alpha, Beta, etc.  How can I solve this problem in both controls?

  Also, I'd like to be able to sort the TreeView by group/detail (the way that windows sorts folders).  How can I implement this?

  This is something I'd like to get answered quickly, so 500 points.  Thanks!

Gavin
0
Comment
Question by:gmurphy79
[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
3 Comments
 
LVL 23

Accepted Solution

by:
rama_krishna580 earned 900 total points
ID: 12061500
0
 
LVL 5

Assisted Solution

by:tzxie2000
tzxie2000 earned 600 total points
ID: 12061584
add column click event by

private void ColumnClick(object o, ColumnClickEventArgs e){
           // Set the ListViewItemSorter property to a new ListViewItemComparer object.
         this.listView1.ListViewItemSorter = new ListViewItemComparer(e.Column);
           // Call the sort method to manually sort the column based on the ListViewItemComparer implementation.
         listView1.Sort();
      }

or you just want call sort by yourself

this.listView1.ListViewItemSorter = new ListViewItemComparer(e.Column);
listView1.Sort();

write a ItemCompareer class to implement IComparer

class ListViewItemComparer : IComparer {
      private int col;
      public ListViewItemComparer() {
          col=0;
      }
      public ListViewItemComparer(int column) {
          col=column;
      }
      public int Compare(object x, object y)
      {
          string valuex,valuey;
          int res;

          valuex=(((ListViewItem)x).SubItems[col].Text;
          valuey=(((ListViewItem)y).SubItems[col].Text;

          if(IsNum(valuex))
          {
               if(IsNum(valuey))
                    res=int.Parse(valuex).CompareTo(int.Parse(valuey));
              else
                    res=-1;
          }
         else
         {
               if(isNum(valuey))
                     res=1;
               else
                    res=valux.CompareTo(valuey);
          }
          return res;
      }
   }

0
 

Author Comment

by:gmurphy79
ID: 12084569
Thanks both of you.  It looks like I need to write a class that implements IComparer and then assign it to the listviewitemsorter.  I assume I can do the same for a treeview (although I haven't had time to do this coding yet).  Thanks!
0

Featured Post

Independent Software Vendors: 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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

801 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